Skip to content

Instantly share code, notes, and snippets.

@finwe
Created May 18, 2012 08:48
Show Gist options
  • Save finwe/2724054 to your computer and use it in GitHub Desktop.
Save finwe/2724054 to your computer and use it in GitHub Desktop.
Array of mixed (usualy objects) to indexed array via array_reduce
<?php
array_reduce(
$objects, // array of objects, arrays...
function ($array, $item) {
$array[$item->getId()] = $item; // $item is current element
return $array; // $array is whole array that will be returned
},
array() // begin with empty array - strict standards
);
// Copy & paste friendly version
array_reduce($objects, function ($array, $item) {
$array[$item->getId()] = $item;
return $array;
}, array());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment