2016-02-09 25 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