2016-02-09 53 views
1

我有數組並且不想分組一些數值。 我輸入數組:組數組並且總結一些數值

$input = 
(
    [0] => Array (
     [shipping] => Array 
     (
      [ups] => Array 
       (
        [0] => Array 
        (
         [name] => Next Day Air (early AM) 
         [code] => 14 
         [price] => 110.00 
        ) 

        [1] => Array 
        (
         [name] => Next Day Air 
         [code] => 01 
         [price] => 105.25 
        ) 
       ) 
     ) 
    ) 
    [1] => Array (
     [shipping] => Array 
     (
      [ups] => Array 
       (
        [0] => Array 
        (
         [name] => Next Day Air (early AM) 
         [code] => 14 
         [price] => 120.00 
        ) 

        [1] => Array 
        (
         [name] => Next Day Air 
         [code] => 01 
         [price] => 105.50 
        ) 
       ) 
     ) 
    ) 
); 

我想要得到的東西是這樣的:

$shipping_group = array (
    [ups] => Array 
     (
     [0] => Array 
      (
       [name] => Next Day Air (early AM) 
       [code] => 14 
       [price] => 230.00 
      ) 
     [1] => Array 
      (
       [name] => Next Day Air 
       [code] => 01 
       [price] => 210.75 
      ) 
    ) 
); 

我曾嘗試:

$shipping_ups_total = array(); 

foreach($input as $box) { 
    $box = (object)$box; 

    if (!empty($box->shipping['ups'])) { 
     foreach($box->shipping['ups'] as $m=>$method) { 
      $price += $method['price']; 
      $shipping_ups_total[$m]  = array(
       'name' => $method['name'], 
       'code' => $method['code'], 
       'price' => $price 
      ); 
     } 
    } 
} 

但我有錯誤的結果...

回答

1

如果你想給他們組通了code索引值,然後通過使用它作爲關鍵字來實現。用它來分組和彙總你想要的值。 (在這種情況下,價格)。

首先初始化只是一個容器,然後不斷地在循環中添加+=

$shipping_group['ups'] = array(); 
foreach($input as $box) { 
    foreach($box['shipping']['ups'] as $ups) { 
     // initialize 
     $code = $ups['code']; // create your key 
     if(!isset($shipping_group['ups'][$code])) { 
      // initialize initial container, start the price from zero 
      $shipping_group['ups'][$code] = array('name' => $ups['name'], 'code' => $code, 'price' => 0); 
     } 
     // after that, just continually add 
     $shipping_group['ups'][$code]['price'] += $ups['price']; 
    } 
} 

注:這將添加代碼爲主體的分組陣列上的一個鍵。

如果你想採取了只使用array_values

$shipping_group['ups'] = array_values($shipping_group['ups']); 

Sample Output

+0

謝謝你!!!!! – user889349

+0

@ user889349當然很高興這有幫助 – Ghost

0

我想這樣嵌套的東西會工作。

循環提供的每個陣列。循環遍歷內部數組。循環送貨信息並將送貨類型(索引)推送到shipping_groups陣列上。該值將是該數組中的信息。

$shipping_groups = array(); 
foreach($input as $arr){ 
    foreach($arr as $details){ 
     foreach($details as $shipping_type => $info){ 
      $shipping_types[$shipping_type]['price'] = number_format($info['price'] + $shipping_types[$shipping_type]['price'], 2, '.',','); 
     } 
    } 
} 

而且將產生如下,請注意我的數據也不會精確地反映你的,但它會在結構上正確的:

array(3) { 
    ["ups"]=> 
    array(1) { 
    ["price"]=> 
    string(6) "114.37" 
    } 
    ["fedex"]=> 
    array(1) { 
    ["price"]=> 
    string(5) "87.18" 
    } 
    ["dhl"]=> 
    array(1) { 
    ["price"]=> 
    string(5) "61.47" 
    } 
} 
+0

感謝。但我需要總結UPS的每種方法的價格。 – user889349

+0

@ user889349哦!我的錯誤,我會給你一個重擊,堅持下去。 – Ohgodwhy

+0

@ user889349好的,朋友。現在看看,應該爲你擺脫。 – Ohgodwhy