【问题标题】:Array of categories from array of products来自产品数组的类别数组
【发布时间】:2013-02-04 12:53:21
【问题描述】:

我有一个产品数组,其中包含按显示顺序排序的产品列表。

$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 没有足够深入地检查类别数组。有人对解决此问题的最佳方法有任何建议吗?非常感谢

【问题讨论】:

    标签: php arrays for-loop categories product


    【解决方案1】:

    $categories[$product['category']][] = $product 的想法是正确的。您需要检查的是$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)
        );
    }
    

    这会给你一个数组,格式如下:

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

    【讨论】:

      【解决方案2】:

      也许你应该使用 array_key_exists() 而不是 in_array()。 http://php.net/manual/en/function.array-key-exists.php

      $product['cateogry'] 上有一个错字

      【讨论】:

        【解决方案3】:
        $categories  = array();
        foreach($products as $product){
            $categories[$product['category']]['title']                           = $product['category'];
            $categories[$product['category']]['products'][$product['productID']] = $product['title'];
        }
        
        print_r($categories);
        

        【讨论】:

        • $categories[$product['category']]['products']$product[['productID']] = $product['title'];应该是$categories[$product['category']]['products'][$product['productID']] = $product['title'];
        【解决方案4】:

        你可以这样使用:

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

        这会将它们放入您想要的数组中。

        【讨论】:

          【解决方案5】:
          <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);
          ?>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2023-03-23
            • 2016-03-20
            • 1970-01-01
            • 2020-11-28
            • 1970-01-01
            • 2018-09-06
            • 1970-01-01
            • 2012-11-03
            相关资源
            最近更新 更多