如何使用数组PHP在具有两个数组的类别下生成多个产品的数组

时间:2018-03-06 16:46:04

标签: php arrays

我必须尝试制作那种类型的数组但不要制作预期的数组。我有两个数组,一个是类别,一个是产品数组。我想组合两个数组,我需要显示特定类别下的所有产品。 下面的类别和产品数组:

$categories = [ 2490 => "POS",2485 => "Online Trainings",2484 => "Onsite Trainings"];
       $product = [
2490=> [ 
    "data"=>[
      0 =>  [
         "prd_id" => "7222",
         "prd_price" => "$21",
         "product_data" => [
              0=>[
                 "prd_sku" => 12,
                 "prd_name" => "Export Ready Program" 
                ]
             ]
      ],
      1=>[
         "prd_id" => "7221",
         "prd_price" => "$25",
         "product_data"=>[
              0=>[
               "prd_sku" => 123 ,      
               "prd_name" => "Workplace Assured",

               ]
             ]
      ]
    ]
 ],
 2485=>[
   "data"=>[
       0=>[
        "prd_id" => "7224",
        "prd_price" => "$45",
        "product_data"=>[
            0=>[
               "prd_sku" => 123 ,  
               "prd_name" => "Exporting Documentation"
              ]
            ]
       ]
    ]
 ],
  2484 =>[
     "data"=>[ 0=>[
        "prd_id" => "7223",
        "prd_price" => "$15",
        "product_data"=>[
            0=>[
                "prd_sku" => 124 ,  
               "prd_name" => "clock"
              ]
            ]
      ]
    ]
]
];

我的代码:

$finalArray = [];
foreach($product AS $key=>$data){
    $firstArray = [];
    $firstArray['category_id'] = $key;
    $firstArray['category_name'] = $categories[$key];
    $i = 1;
    foreach($data AS $k=>$datum){
        $firstArray['product_category_listng'][$i]['product_id'] = $datum['prd_id'];
        $firstArray['product_category_listng'][$i]['product_name'] = $datum['prd_name'];
        $finalArray = $firstArray;
        $i++;
    }
}
print_r($finalArray);

我的预期输出应为:

   Array
(
    [2490] => Array
        (
            [category_id] => 2490
            [category_name] => POS
            [product_category_listing] => Array
                (
                    [0] => Array
                        (
                            [prd_id] => 7222
                            [prd_name] => Export Ready Program
                        )

                    [1] => Array
                        (
                            [prd_id] => 7221
                            [prd_name] => Workplace Assured
                        )
                )
        )

    [2485] => Array
        (
            [category_id] => 2485
            [category_name] => Online Trainings
            [product_category_listing] => Array
                (
                    [0] => Array
                        (
                            [prd_id] => 7224
                            [prd_name] => Exporting Documentation
                        )
                )
        )

    [2484] => Array
        (
            [category_id] => 2484
            [category_name] => Onsite Trainings
            [product_category_listing] => Array
                (
                    [0] => Array
                        (
                            [prd_id] => 7223
                            [prd_name] => clock
                        )
                )
        )
)

请帮忙 在此先感谢!!

1 个答案:

答案 0 :(得分:1)

它比您在代码中所做的更简单。您可以使用category_id作为索引:

foreach($product as $category => $values) {
    $result[$category]['category_id'] = $category;
    $result[$category]['category_name'] = $categories[$category];
    $result[$category]['product_category_listing'] = $values;
}

或动态分配索引:

foreach($product as $category => $values) {
    $result[] = ['category_id' => $category,
                 'category_name' => $categories[$category],
                 'product_category_listing' => $values];
}

第一个示例yield(第二个示例索引将为0,1,2):

Array
(
    [2490] => Array
        (
            [category_id] => 2490
            [category_name] => POS
            [product_category_listing] => Array
                (
                    [0] => Array
                        (
                            [prd_id] => 7222
                            [prd_name] => Export Ready Program
                        )

                    [1] => Array
                        (
                            [prd_id] => 7221
                            [prd_name] => Workplace Assured
                        )
                )
        )

    [2485] => Array
        (
            [category_id] => 2485
            [category_name] => Online Trainings
            [product_category_listing] => Array
                (
                    [0] => Array
                        (
                            [prd_id] => 7224
                            [prd_name] => Exporting Documentation
                        )
                )
        )

    [2484] => Array
        (
            [category_id] => 2484
            [category_name] => Onsite Trainings
            [product_category_listing] => Array
                (
                    [0] => Array
                        (
                            [prd_id] => 7223
                            [prd_name] => clock
                        )
                )
        )
)