2015-04-04 175 views
1

我有陣列格式爲:如何將多維數組轉換爲一維數組?

Array ([0] => Array ([PRODUCT_ID] => 40) [1] => Array ([QUANTITY] => 2) [2] => Array ([PIECE_BAG] => 3) [3] => Array ([TOTAL_QUANTITY] => 2) [4] => Array ([UNIT_PRICE] => 3) [5] => Array ([TOTAL_PRICE] => 6) [6] => Array ([AVAILABILITY] => 4)) 

而且我想:

Array ( [PRODUCT_ID] => 40 [QUANTITY] => 2) 
+0

你有一些事先與你的問題? – 2015-05-01 18:48:29

回答

1

您需要通過$array[0]$array[1]第一迭代,並讓您在foreach循環所需要的價值。看到代碼:

<?php 
$array = Array (
0 => Array ('PRODUCT_ID' => 40), 
1 => Array ('QUANTITY' => 2), 
2 => Array ('PIECE_BAG' => 3), 
3 => Array ('TOTAL_QUANTITY' => 2), 
4 => Array ('UNIT_PRICE' => 3), 
5 => Array ('TOTAL_PRICE' => 6), 
6 => Array ('AVAILABILITY' => 4)); 

$new_array = array(); 

for($i = 0; $i < 2; $i++) { 
    foreach($array[$i] as $key => $value) { 
    $new_array[$key] = $value; 
    } 
} 

print_r($new_array); 

輸出:

Array 
(
    [PRODUCT_ID] => 40 
    [QUANTITY] => 2 
) 
相關問題