When Loops Attack
August 17th, 2009
1 comment
Let’s say you have an array of values, and you want to loop through those values. You might do something like this:
$values = array(...);
foreach ($values as $value) { ... }
Simple, right? So simple, in fact, that I can’t figure out why I keep seeing this instead:
$tmp = array(...);
foreach ($tmp as $t) {
$values[$t] = 1;
}
foreach (array_keys($values) as $value) { ... }
This takes the values of one array and uses them as the keys for a second array, but then pulls out the array keys and operates on them alone, disregarding the value! It’s not an isolated incident, either, as it’s come up in numerous spots in the code. If anyone can think of why it would be done this way, please let me know because I’ve got nothing.
Categories: General Programming, PHP