-2
我有2個數組,產品類型和產品,我試圖製作一個多維數組,將產品放入主數組中的自己的product_type數組中。這些產品有一個我可以匹配的product_type值。我使用php和mysql來獲取信息。我的問題是,如果我有多個產品分配給一個product_type,那麼它只顯示其中一個產品。感謝您提供任何幫助!來自2個陣列的多維數組
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和產品數組 –
我刪除了mysql標籤。這個問題與MySQL無關。我知道你說你從MySQL獲得了數據,但這對於這個問題並不重要。 –
「它只顯示其中之一」。你確定嗎? [見這裏](http://sandbox.onlinephpfunctions.com/code/933506565b99af75830329584dc51f71e4b96866)顯示您使用/顯示結果的代碼。 –