Today I run across this strange PHP behaviour. Say, you need to build an associative array from an array. For a task like that, I wrote this:
$idx=0;
foreach($planets as $planet)
$results[$idx++] = $planet;
Guess what? It won’t work. $result is built as a normal array, not associative. And it could be reasonable, after all… but:
$idx=0;
foreach($planets as $planet)
{
$idx++;
$results[$idx] = $planet;
}
Note the increment of $idx wrapped out from the square brackets. Well, after the loop, $result is a shiny new associative array. Ah!
Additional comments powered by BackType