Created
May 18, 2012 08:48
-
-
Save finwe/2724054 to your computer and use it in GitHub Desktop.
Array of mixed (usualy objects) to indexed array via array_reduce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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