I have an array that looks like the following:
Array (
[0] => Array ( [id] => 10 [value] => 5 ) [1] => Array ( [id] => 10 [value] => 1 ) [2] => Array ( [id] => 11 [value] => 1 ) [3] => Array ( [id] => 11 [value] => 1 ))
How can I consolidated the array by id? The resulting array I’m looking for would be something like:
Array (
[0] => Array ( [id] => 10 [value] => 6 ) [1] => Array ( [id] => 11 [value] => 2 ))
This is not a very efficient structure. Have you considered consolidating it in this form?
That would allow fast key lookup on ID.
TO consolidate your first array into that form, just do this:
Which would give you an array in the form of:
If you really need it in your requested form, one more processing loop will get it there…
So, there you go!
And more compact: