[英]Multidimensional array from 2 arrays
我有 2 個數組,product_types 和 products,我正在嘗試制作一個多維數組,將產品放入主數組中的它們自己的 product_type 數組中。 產品有一個 product_type 值,我可以與之匹配。 我正在使用 php 和 mysql 來獲取信息。 我的問題是,如果我將多個產品分配給一個 product_type,那么它只顯示其中之一。 感謝您提供任何幫助!
foreach ($type_array as $key => $type):
foreach ($product_array as $k =>$product):
if($product['product_type'] == $type){
$variables[$type][] = $product;
}
endforeach;
endforeach;
我的陣列:
Array
(
[0] => Getting Skin Ready: Cleanse
[1] => Getting Skin Ready: Scrub
[2] => Getting Skin Ready: Oil Control/Tone
[3] => Treatment
[4] => Hydration and Calming
[5] => Sun Protection
[6] => Enhancers
[7] => Programs and Kits
)
Array
(
[0] => Array
(
[product_id] => 10
[product_name] => 050100 NormacleanseTM Cleanser for Normal to Dry Skin
[product_price] => 26.95
[product_size] => 240 mL / 8 Fl. Oz.
[product_type] => Sun Protection
)
[1] => Array
(
[product_id] => 11
[product_name] => 050002 OilacleanseTM Cleanser for Normal to Oily Skin
[product_price] => 26.95
[product_size] => 240 mL / 8 Fl. Oz.
[product_type] => Getting Skin Ready: Cleanse
)
[2] => Array
(
[product_id] => 12
[product_name] => 052200 FoamacleanseTM Gentle Foaming Cleanser
[product_price] => 26.95
[product_size] => 240 mL / 8 Fl. Oz.
[product_type] => Treatment
)
[3] => Array
(
[product_id] => 13
[product_name] => 060500 Offects® Hydrating Cleanser Normal to Dry
[product_price] => 21.45
[product_size] => 150 mL / 5 Fl. Oz.
[product_type] => Getting Skin Ready: Scrub
)
[4] => Array
(
[product_id] => 14
[product_name] => 060402 Offects® Exfoliating Cleanser Normal to Oily
[product_price] => 21.45
[product_size] => 150 mL / 5 Fl. Oz.
[product_type] => RX Products
)
)
上面代碼的輸出:
Array
(
[Getting Skin Ready: Scrub] => Array
(
[0] => Array
(
[product_id] => 13
[product_name] => 060500 Offects® Hydrating Cleanser Normal to Dry
[product_price] => 21.45
[product_size] => 150 mL / 5 Fl. Oz.
[product_type] => Getting Skin Ready: Scrub
)
)
[Treatment] => Array
(
[0] => Array
(
[product_id] => 12
[product_name] => 052200 FoamacleanseTM Gentle Foaming Cleanser
[product_price] => 26.95
[product_size] => 240 mL / 8 Fl. Oz.
[product_type] => Treatment
)
)
[Sun Protection] => Array
(
[0] => Array
(
[product_id] => 10
[product_name] => 050100 NormacleanseTM Cleanser for Normal to Dry Skin
[product_price] => 26.95
[product_size] => 240 mL / 8 Fl. Oz.
[product_type] => Sun Protection
)
)
)
這實際上是有效的。 我需要選擇更多的 product_types。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.