Assuming you want key / value pairs to be in each / other pattern, you can use:
$data = array('x', 'foo', 'y', 'bar', 'z'); $new = array(); $end = count($data); for ($i = 0; $i < $end; $i += 2) { $new[$data[$i]] = (isset($data[$i + 1]) ? $data[$i + 1] : ''); } print_r($new);
It gives:
Array ( [x] => foo [y] => bar [z] => )
This will jump to your data list and set the first value as the key and the next as the value. If there is no βnextβ value (that is, the Finite element in the original array, which is not divisible by 2), it is empty.
A caveat to this approach is that if the same βkeyβ is examined more than once, it will be overwritten. This can be circumvented by adding if (isset($new[$data[$i]])) continue; as the first line in the loop.
source share