$colors = array(
'r' => 'a',
'g' => 'b',
'b' => 'c'
);
list($v, $k) = each($colors);
echo $v . " " . $k
Now the above prints r a which I'm surprise since I thought the 'list' construct only works for numerical array. Why does it work?
Yes you are right list() only works on numerical arrays and assumes the numerical indices start at 0. but he reason why your code is working its because of
each
.each
traverse an array
therefore converted thekeys
tonumerical
example if you run the following :Output
You can use that
array('r' => 'a');
has been converted toarray(0 => 'r', 1 => 'a');
therefore you can now uselist
since they now havenumerical keys
FROM PHP DOC
Also