XTRUST.ORG
XTRUST.ORG

Reputation: 3392

Group array and summarize some values

I have array and wan't to group some values. My input array:

$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
                     )
                )
          )
    )
);

I'm trying to get something like this:

$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
            )
      )
);

What I have tried:

$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
            );
        }
    }
}

But I have wrong results...

Upvotes: 1

Views: 47

Answers (2)

Kevin
Kevin

Reputation: 41885

If you want to group them thru the code index value, then do so by using a that as a key. Use that to group and aggregate the values that you want. (In this case, the price).

First just initialize a container, then continually add += in the loop:

$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'];
    }   
}

Note: This will add the code as a key on the main grouping array.

If you want to take that out just use array_values:

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

Sample Output

Upvotes: 1

Ohgodwhy
Ohgodwhy

Reputation: 50787

I guess something nested like this would work.

Loop each of the arrays provided. Loop over the inner array. Loop the shipping information and push the shipping type (the index) onto the shipping_groups array. The value will be the info within that array.

$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, '.',',');
        }
    }
}

And will produce the following, note my data will not exactly mirror yours, but it will be structurally correct:

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"
  }
}

Upvotes: 0

Related Questions