php - How to convert an array into a key,value,... array? -


i have array:

array (     [0] => item     [1] => repaired wattles     [2] => types     [3] => non-wire     [4] => estimated qty     [5] => 124     [6] => actual qty     [7] => 124     [8] => upload file     [9] => example.jpg ) 

i need add next value previous. need this

array (     [item] => repaired wattles     [types] => non-wire     [estimated qty] => 124     [actual qty] => 124     [upload file] => example.jpg  ) 

i have along lines of this:

$array = array(     foreach($stri $string) {         $stri[] => $stri[$val]     $val = $string + 1; ); 

i know wrong. right here i'm stuck , don't know how code working want to.

write simple for loop , increment counter 2 in each loop:

$result = array(); ($i = 0; $i < count($arr); $i += 2) { // increment counter +2     if (isset($arr[$i]) && isset($arr[$i+1])) { // make sure if both indexes exists in array         $result[$arr[$i]] = $arr[$i+1];     } }  

usage examples:

$arr = array('aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'); // ... var_dump($result);  array(3) {   'aaa' =>   string(3) "bbb"   'ccc' =>   string(3) "ddd"   'eee' =>   string(3) "fff" } 

$arr = array('aaa', 'bbb', 'ccc', 'ddd', 'eee'); // ... var_dump($result);  array(2) {   'aaa' =>   string(3) "bbb"   'ccc' =>   string(3) "ddd" } 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -