简体   繁体   English

产品系列中的类别列表

[英]Array of categories from array of products

I have a product array containing a list of products sorted by their display order. 我有一个产品数组,其中包含按其显示顺序排序的产品列表。

$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

I'm trying to figure out the logic of turning it into a category array like this: 我试图弄清楚将其转换为这样的类别数组的逻辑:

$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"
        ) 
)

So for each product I have: 因此,对于每种产品,我都有:

if (!in_array($product['cateogry'], $categories)){
    $categories[] = $product['cateogry'];
    $categories[$product['category']][] = $product; 
} else {
    $categories[$product['category']][];
}

But this isn't working, because I don't think in_array is checking deep enough into the category array. 但这是行不通的,因为我认为in_array没有深入检查类别数组。 Does anyone have any advice on the best way to solve this? 有人对解决此问题的最佳方法有任何建议吗? Many thanks 非常感谢

You have the right idea with $categories[$product['category']][] = $product . 您有$categories[$product['category']][] = $product的正确想法。 What you need to be checking is if the key $product['category'] exists in $categories : 您需要检查的是键$product['category']存在于$categories

if (array_key_exists($product['category'], $categories)) {
    $categories[$product['category']]['products'][] = $product['title'];
} else {
    // initialize category data with first product
    $categories[$product['category']] = array(
        'title' => $product['category'],
        'products' => array($product)
    );
}

This will give you an array in the form: 这将为您提供以下形式的数组:

$categories = array(
   "CD" => array(
        "title" => "CD",
        "products" => array (
            [0] => "Spice Girls Album",
            [1] => "The Beatles Album"
        ) 
    ),
   "DVD" => array(
        "title" => "DVD",
        "products" => array (
            [0] => "Snakes on a plane"
        ) 
)

Maybe you should use array_key_exists() instead of in_array(). 也许您应该使用array_key_exists()而不是in_array()。 http://php.net/manual/en/function.array-key-exists.php http://php.net/manual/zh/function.array-key-exists.php

There's a typo on $product['cateogry'] $ product ['cateogry']上有一个错字

$categories  = array();
foreach($products as $product){
    $categories[$product['category']]['title']                           = $product['category'];
    $categories[$product['category']]['products'][$product['productID']] = $product['title'];
}

print_r($categories);

You could use something like this: 您可以使用如下形式:

$new_products = array();
foreach ($products as $product) {
  $new_products[$product['category']][] = $product['title'];
}

That would put them into an array like you want. 这样会将它们放入所需的数组中。

<pre>
<?php
$p[] = array(productID => 189736,title => 'Spice Girls Album', category => 'CD', order => 0);
$p[] = array(productID => 23087, title => 'Snakes on a plane', category => 'DVD', order => 0);
$p[] = array(productID => 9874, title => 'The Beatles Album', category => 'CD', order => 1);
foreach($p as $p){
    $c[$p['category']]['title'] = $p['category'];
    $c[$p['category']]['products'][] = $p['title'];
}
print_r($c);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM