我想按顏色先排序然後按類型排序。根據Laravel中的多個條件對集合進行排序
我在想也許雙foreach循環可能不是答案?
以下是我到目前爲止的內容。
private $color_order = [
'white',
'green',
'purple',
'blue',
''
];
private $type_order = [
'diamond',
'square',
'circle'
];
private function sortCards($cards, $color_order, $type_order) {
$collection = collect($cards);
return $collection->sortBy(function($card) use ($color_order, $type_order) {
foreach ($type_order as $index => $type) {
foreach ($color_order as $index => $color) {
if ($card->card_colors == $color && str_contains($card->card_type, $type)) {
return $index;
}
}
}
})->values();
}
$ cards有什麼價值? –