I have an array with objects like below:
array:113 [
0 => App\Api\Wheel {#5809
-manufacturer: "RIAL"
-colour: "diamant"
-design: "aaaaaa"
-surface: "ss"
-diameter: 16
}
1 => App\Api\Wheel {#6201
-manufacturer: "RIAL"
-colour: "diamant"
-design: "aaaaaa"
-surface: "ss"
-diameter: 17
}
Here, based on the value, I need to group. Diameter is going to be different and others are going to be same.
My final result is to be same structure but diameter in array or comma separated string.
array:113 [
0 => App\Api\Wheel {#5809
-manufacturer: "RIAL"
-colour: "diamant"
-design: "aaaaaa"
-surface: "ss"
-diameter: [
16,
17
]
}
So far, I have tried following lines of code,
$finalResult = array();
foreach ($records as $record) {
if (!in_array($record->getManufacturer(), array_keys($finalResult))) {
$finalResult[$record->getManufacturer()] = (object)array(
'manufacturer' => $record->getManufacturer(),
'colour' => $record->getColour(),
'design' => $record->getDesign(),
'surface' => $record->getSurface(),
'diameter' => $record->getDiameter()
);
} else {
$finalResult[$record->getManufacturer()]->diameter = [$record->getDiameter()];
}
}
dd(($finalResult));
But, I can not able to group by or return result into same format.
Can any body please help me?