I'm wanting to group my data by a specific value (parent name) and then merge all the items that share the same parent name under a "items" array
However it's overwriting the items array, not adding to it, so for example "items" in the output should have multiple items not just one.
Any ideas?
$result = array();
foreach ($page->products_codes as $option) {
$result[$option->parent->name]["title"] = $option->parent->title;
$result[$option->parent->name]["items"] = $option->title;
}
Outputs as:
array (
'fixture' =>
array (
'title' => 'Fixture',
'items' => 'Pinhole90 Fixed with LED51',
),
'finish' =>
array (
'title' => 'Finish',
'items' => 'RAL',
),
'ip-rating' =>
array (
'title' => 'IP Rating',
'items' => 'IP54',
),
'emergency' =>
array (
'title' => 'Emergency',
'items' => 'Maintained 3hr Self Test',
),
'installation' =>
array (
'title' => 'Installation',
'items' => 'Plaster-kit for seamless flush appearance',
),
'led' =>
array (
'title' => 'LED',
'items' => 'LED50 ONE',
),
'cct' =>
array (
'title' => 'CCT',
'items' => '90 CRI 4000K',
),
'beam-angle' =>
array (
'title' => 'Beam Angle',
'items' => '38°',
),
'protocol' =>
array (
'title' => 'Protocol',
'items' => 'Bluetooth',
),
'louvre-lens' =>
array (
'title' => 'Louvre/Lens',
'items' => 'Heavy Spread Lens',
),
)
Any thoughts?
CodePudding user response:
Based on the preferred data structure you specified:
$result = array();
foreach ($page->products_codes as $option) {
$result[$option->parent->name]["title"] = $option->parent->title;
$result[$option->parent->name]["items"][] = $option;
}
$result = array_values($result);
Here's a working example: https://3v4l.org/u9XBk