1

表示順序でソートされた製品のリストを含む製品配列があります。

$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がカテゴリ配列を十分に深くチェックしているとは思わないため、これは機能していません。誰かがこれを解決するための最良の方法について何かアドバイスがありますか?どうもありがとう

4

5 に答える 5

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"
        ) 
)
于 2013-02-04T12:59:36.940 に答える
0
<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);
?>
于 2013-02-04T13:06:20.857 に答える
0

たぶん、in_array()の代わりにarray_key_exists()を使用する必要があります。 http://php.net/manual/en/function.array-key-exists.php

$product['cateogry']にタイプミスがあります

于 2013-02-04T12:57:52.353 に答える
0
$categories  = array();
foreach($products as $product){
    $categories[$product['category']]['title']                           = $product['category'];
    $categories[$product['category']]['products'][$product['productID']] = $product['title'];
}

print_r($categories);
于 2013-02-04T13:01:18.920 に答える
0

次のようなものを使用できます。

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

それはあなたが望むようにそれらを配列に入れるでしょう。

于 2013-02-04T13:04:21.917 に答える