0

I've got this multi-dimensional array (if that's what it is called) of customers and inside the customer is his items, and the fee for those items.. However, I need to get all the fee's from the items and sum them for the customer. Array Structure

    Array
    (
        [6bb728] => Array
            (
                [TitleName] => Sir Isaac
                [Surname] => Newton
                [sum] =>
                [id] => 6bb728
                [items] => Array
                    (
                        [29] => Array
                            (
                                [AssignedTo] => 6bb728
                                [Number] => 03-014
                                [MFee] => 103.5
                            )

                    )

            )

The sum key should contain the sum of the items fees. Which brings me on to my question: Is there any efficient way of getting the sum of one key which is present in a multi dimensional array?

I've tried a quadruple foreach which sent my computer into a loop (there are hundreds of customers and it'd have to do so many loops)

foreach($customers as $value) {
    foreach($value as $value1){
        foreach($value1['items'] as $value2) {
            foreach($value2 as $value3) {
                $customers[$value3['AssignedTo']]['Fee'] += $value3['MFee'];
            }
        }
    }
}
PwnageAtPwn
  • 431
  • 1
  • 6
  • 21

2 Answers2

2

Looks like you could reduce it to:

foreach($customers as $customer) {
    foreach($customer['items'] as $item) {
        $customers[$item['AssignedTo']]['Fee'] += $item['MFee'];
    }
}

Edit: actually you might be able to make that assignment a little less confusing too:

$customer['Fee'] += $item['MFee'];
Gareth
  • 5,693
  • 3
  • 28
  • 37
  • Works perfectly! A bit of a speed decrease, but that is to be expected with the huge array I'm feeding it. – PwnageAtPwn Apr 12 '13 at 09:22
  • 1
    @PwnageAtPwn Yeah, nested loops are always going to be a bit rubbish performance-wise. If speed becomes an issue you'll probably have to look at changing the array structure slightly so you can use something like [`array_sum`](http://php.net/manual/en/function.array-sum.php). – Gareth Apr 12 '13 at 09:30
0

Use multiple loops rather than nested loops while looping over customers.

eg:

<?php
$input_array = array('a' => 'aaa', 'b' => 'bbb', 'c' => 'ccc', 'd' => 'dddd', 'e' => 'eeee');

//chunks of size 2
$my_array_chunks = array_chunk($input_array, count($input_array) / 2, true);
?>

//array chunks formed

Array
(
  [0] => Array
        (
            [a] => aaa
            [b] => bbb
        )

    [1] => Array
        (
            [c] => ccc
            [d] => dddd
        )

    [2] => Array
        (
            [e] => eeee
        )

)

divided across multiple loops

<?php
foreach(my_array_chunks[0] as $val){
   //code here
}
foreach(my_array_chunks[1] as $val){
      //code here
}

foreach(my_array_chunks[2] as $val){
     //code here
}

Why is one loop so much slower than two loops?

Community
  • 1
  • 1
Angelin Nadar
  • 8,944
  • 10
  • 43
  • 53