Convert array to associative array with specific key and value [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to convert an array into associative array as first element should act as a key and second element should act as its value ?please tell me how i can do that

If you have only two elements (first and second as you mentioned), then you can do simply like this
$assoc = array($simple[0] => $simple[1]);
If you wanted to convert pairs of values like [1,2,3,4] to [1=>2,3=>4], then use this code snippet
$assoc = array();
for ($i = 0; $i < count($simple); $i=$i+2) {
$assoc[$simple[$i]] = $simple[$i+1];
}

Related

PHP: What is the best way to get last even value in array? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
Improve this question
Suppose , I have a array with some integers value (odd and even both mixed) in php.and I want to get last even number.
A loop that works backwards through the array would work.
$number=false;
$a=array(2,4,5,4,6,7,13,11,95,88,16,17,107);
for($i=count($a)-1; $i >= 0; $i--){
if( $a[$i] % 2 == 0 ){
$number=$a[$i];
break;
}
}
echo $number; //16

I have two arrays. In first array i have some null values and i want to update these null values with second array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
In this I have two arrays, In the first array I have some key have null values in the array. in the second array, I have also the same number of keys in arrays as having null values in first, I want to replace the first array key null values with the second array values
$arr1=array(0=>array('quantity'=>1),1=>array(),2=>array('quantity'=>3),3=>array(),4=>array());
$arr2=array (0 =>array(),1 =>array (),2 =>array(0 =>array('quantity'=>2)));
$result_array_needed=array(0=>array('quantity'=>1),1=>array(),2=>array('quantity'=>3),3=>array(),4=>array(0 =>array('quantity'=>2)));
Nick answer is good. I think you can simplify it using array-shift:
foreach ($arr1 as &$arr)
if (empty($arr)) $arr = array_shift($arr2);
Live example: 3v4l

Dissect, sort and compare arrays [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have the two following arrays:
a:4:{i:0;s:2:"98";i:1;s:2:"98";i:2;s:2:"89";i:3;s:2:"99";}
a:4:{i:0;s:11:"Musculation";i:1;s:3:"Gym";i:2;s:22:"Production in HTML/CSS";i:3;s:9:"Endurance";}
Each array has 4 values that are correlated. I want to display only three values from second array that have the highest correlating number values from first array:-
Endurance - 99
Musculation - 98
Gym - 98
How do I achieve this?
Use array_multisort to sort the text according to the corresponding numbers
array_multisort($numbers, SORT_DESC, SORT_NUMERIC, $text);
Take the first three values.
$result = array_slice($text, 0, 3);
If you want to show the numbers with the text, the keys will still match up, so you can iterate the text array and use its key to get the right value from the number array.
foreach ($text as $key => $title) {
echo "$title: $numbers[$key]\n";
}

How to get certain part of a string value in php? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I get one value like "13and45" and would like to know if there is a way so I can store these values separately like
$string="13and45";
$value1=13;
$value2=45;
Do you guys know how I can manipulate the string in order to get something like what I have above?
PHP - Explode () function
This will return an array with the two values.
Example:
$array = explode ("and", $string);
Returned array with strings:
$array[0] = 13
$array[1] = 45
You can use something like intval(substr($string, 0, 2) to get the first integer and intval(substr($string, 5, 2) for the second. intval will get the integer value from a string, and substr will get the portion of the string given by ($string, index, length).

If (Event) then set array to nothing? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a MySQL query that does a selection. I then have a $numrows variable which I have set to $numrows = mysql_num_rows($query);. If the value of $numrows == 0, I want the array variable I have to be set to empty. I tried setting the array variable to null and ""; but those don't work. Any suggestions? Thanks.
Empty an array
$array= array();
Delete it altogether
unset($array);

Categories