I have an array that has indexes that I am trying to combine together. For some reason, I cannot circle my head.
Original array
$seperateArray = json_decode('[
{ "tier1": "Group1", "tier2": "Blue", "tier3": "Round", "tier4": "Harold" },
{ "tier1": "Group1", "tier2": "Blue", "tier3": "Round", "tier4": "Arthur" },
{ "tier1": "Group1", "tier2": "Blue", "tier3": "Round", "tier4": "Tom" },
{ "tier1": "Group2", "tier2": "Blue", "tier3": "Round", "tier4": "Beth" },
{ "tier1": "Group3", "tier2": "Blue", "tier3": "Round", "tier4": "Peter" }]', true);
Turn it on:
{
"Group1": {
"Blue": {
"Round": [
"Harold",
"Arthur",
"Tom"
]
}
},
"Group2": {
"Blue": {
"Round": [
"Peter"
]
}
}
}
That's where I am, but I don’t know if I am moving in the right direction.
$newCombined = array();
foreach($seperateArray as $s) {
if(!array_key_exists($s['tier1'], $newCombined) $newCombined[$s['tier1']] = array();
if(!array_key_exists($newCombined[$s['tier1']][$s['tier2']], $newCombined[$s['tier1']])) $newCombined[$s['tier1']][$s['tier2']] = array();
}
bryan source
share