我有一個產品陣列,其中包含按其顯示順序排序的產品列表。產品陣列中的一系列類別
$products = array(
[0] = array(
"productID" => 189736,
"title" => "Spice Girls Album",
"category" => "CD",
"order" => "0"
),
[1] = array(
"productID" => 23087,
"title" => "Snakes on a plane",
"category" => "DVD",
"order" => "0"
),
[2] = array(
"productID" => 9874,
"title" => "The Beatles Album",
"category" => "CD",
"order" => "1"
), ... etc etc
我試圖找出邏輯把它變成一個數組類像這樣的:
$categories = array(
[0] => array(
"title" => "CD",
"products" => array (
[0] => "Spice Girls Album",
[1] => "The Beatles Album"
)
),
[1] => array(
"title" => "DVD",
"products" => array (
[0] => "Snakes on a plane"
)
)
因此,對於每一個產品,我有:
if (!in_array($product['cateogry'], $categories)){
$categories[] = $product['cateogry'];
$categories[$product['category']][] = $product;
} else {
$categories[$product['category']][];
}
但這ISN不工作,因爲我不認爲in_array正在檢查類別數組的深度。有沒有人有任何建議來解決這個問題的最佳方法?非常感謝
完美,謝謝。 – Lars