I have a JSON array of multiple objects, here's an example:
$people = [{"name":"John", "color":"green"},
{"name":"Mary", "color":"green"},
{"name":"Bob", "color":"red"}]
I use json_decode($people, true) to convert them to an array...
Now let's say I want to combine those that have the same color. I'd have to do array_merge_recursive($people[0], $people[1]) because they both have green as the color. Note that I have to specify which ones I want to merge recursively.
How can I loop through $people after it's been decoded to an array format and automatically merge recursively those that have the same key value?
Something like this:
foreach($people as $person) {
// If a person has same color of previous
// person then merge them recursively.
}
So that I could get this after looping:
[{"name":"John, Mary", "color":"green, green"},
{"name":"Bob", "color":"red"}]