[eluser]xwero[/eluser]
With all the php5.3 talk about closures i wondered what would be more readable, a foreach loop or a array_map call to isolate a single field from every row in a database result.
Code:
$ids = array(); foreach($rows as $row){ $ids[] = $row['id']; }
// VERSUS
$ids = array_map(function ($row){ return $row['id']; },$rows);
The foreach code needs a predefined ids variable to set the default result so the array_map function wins.
But then i did a quick performance test using the CI benchmarking class and the result was foreach : 0.0057 and array_map : 0.0318. I have to mention i didn't do the test using php5.3 so i didn't use a closure but the current syntax.
Code:
function get_id($row){ return $row['id']; }
$ids = array_map('get_id',$rows);
Later today i will do a benchmark with php5.3 to see if the closure is faster than the current syntax.