Let Blick auf unsere ursprünglichen Anordnung von Autos
Array(
[0] => Array
(
[name] => Ford
[color] => Red
[door] => 3
)
[1] => Array
(
[name] => Toyota
[color] => Yellow
[door] => 4
)
[2] => Array
(
[name] => Toyota
[color] => White
[door] => 3
)
[3] => Array
(
[name] => Toyota
[color] => Yellow
[door] => 3
)
[4] => Array
(
[name] => Mercedes
[color] => Black
[door] => 4
)
[5] => Array
(
[name] => Lexus
[color] => Yellow
[door] => 4
)
[6] => Array
(
[name] => Mercedes
[color] => White
[door] => 2
)
[7] => Array
(
[name] => Mercedes
[color] => Black
[door] => 4
)
[8] => Array
(
[name] => Mercedes
[color] => Black
[door] => 4
)
[9] => Array
(
[name] => Toyota
[color] => Black
[door] => 3
)
[10] => Array
(
[name] => Toyota
[color] => White
[door] => 3
)
)
Ausgang Erwartete
Ford
Red | 3
Toyota
Yellow | 4
White | 3
Yellow | 3
Black | 3
White | 3
Mercedes
Black | 4
White | 2
Black | 4
Black | 4
Lexus
Yellow | 4
Code, um dieses Ergebnis
<?php
$carsArray = hydrateCarArray(); // You can ignore this as you already have carsArray this is just to hydrate inital array.
$cleanCarsArray = filterCarsByName($carsArray); // This function sort cars by name
$html = getRequiredFormat($cleanCarsArray); // this hydrate html var with required strcuture
echo $html;
/*
* In your case this function is not required as you already have car array
*/
function hydrateCarArray()
{
$carsName = [
'Mercedes',
'Toyota',
'Lexus',
'Ford',
'Ferari'
];
$carsColors = [
'Red', 'White', 'Black', 'Yellow'
];
for ($counter = 0; $counter <= 10; $counter++) {
$carsArray[] = [
'name' => $carsName[ rand(0, count($carsName) - 1) ],
'color' => $carsColors[ rand(0, count($carsColors) - 1) ],
'door' => rand(2, 4)
];
}
return $carsArray;
}
/*
* Arrange cars array by cars name
*/
function filterCarsByName($carsArray)
{
$cleanCarsArray = [];
foreach ($carsArray as $car) {
$cleanCarsArray[ $car['name'] ][] = $car;
}
return $cleanCarsArray;
}
/*
* This function put the car details in required format.
*/
function getRequiredFormat($cleanCarsArray)
{
$html = '';
array_walk($cleanCarsArray, function ($item, $key) use (&$html) {
$html .= "<br/>" . $key . "<br/>";
array_walk($item, function ($item, $key) use (&$html) {
$html .= $item['color'] . ' | ' . $item['door'] . "</br>";
});
});
return $html;
}
?>
Gehen Sie zuerst durch Ihr Array und gruppieren Sie sie nach der Marke. Verwenden Sie die Marke als Schlüssel für das multidimensionale Array. Grundsätzlich gilt: '[Marke => [Farbe => Betrag, ...], ...]' – Rizier123
Wo wird gedruckt? Verwendet es ein Templating oder ist es nur eine gerenderte Datei am Frontend? Haben Sie einen Beispielcode, der zeigt, wie Sie es rendern? – vsharper
Behebt eine der Antworten Ihr Problem? Wenn ja, wählen Sie bitte die passende Antwort aus. Geben Sie weitere Details/Fragen usw. an. –