我有一个产品数组,其中包含按显示顺序排序的产品列表。
$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']][];
}
但这不起作用,因为我认为 in_array 没有足够深入地检查类别数组。有人对解决此问题的最佳方法有任何建议吗?非常感谢