這裏是我的嵌套的foreach循環從數據庫獲取數據PHP創建嵌套的循環中單獨列
while($attrib_rec = tep_db_fetch_array($attributes)) {
foreach($options_values as $option => $options_value){
foreach($options_value as $options_value_key => $each_value){
//make separate arrays of "option"
print 'option = '.$option.' , value = '.$each_value.'<br />';
}
}
}
我從這些循環得到的輸出是
option = Type , value = GOLD
option = Type , value = SILVER
option = Type , value = BRONZE
option = Purity , value = Rough
option = Purity , value = Neat
option = Purity , value = Mixed
option = Purity , value = Random
option = Model , value = Old
option = Model , value = latest
option = Model , value = GOLD 1.0
option = Model , value = GOLD 1.1
option = Model , value = GOLD 1.2
option = Model , value = GOLD 1.3
我想達到的目標是將每個相同的「選項」值保存到一個數組中。 例如從上面的輸出我應該得到3個數組。即
$type = array('GOLD', 'SILVER', 'BRONZE');
$purity = array('Rough', 'Neat', 'mixed', 'Random');
$model = array('Old', 'Latest', 'GOLD 1.0', 'GOLD 1.1', 'GOLD 1.2', 'GOLD 1.3');
對while循環的每次迭代它應該根據「選項」使數組數量。
如果第一次在while循環的下一次迭代中生成3個數組(如本例中),它可能會生成2個或4個數組,這取決於從DB獲取的相同數量的「Option」。
您正在使用嵌套的'foreach'循環會顯示輸出,就像它顯示的那樣...您可以使用過濾器或'group by' – Lakshya