views:

30

answers:

2
( [0] => Array 
( [0] => Array ( 
[0] => Array ( [price] => 76  ) 
[1] => Array ( [price] => 200  ) 
[2] => Array ( [price] => 500  ) 
[3] => Array ( [price] => 67  ) 

is there a clean way to calculate all these prices

A: 

EDIT: since the arrays contain more than just 'price's, I'll just use a loop:

$total_price = 0;

foreach ($products[0][0] as $product) {
    $total_price += $product['price'];
}

echo $total_price; // 843

Old answer before OP's clarification, assumed only prices existed in the array:

echo array_sum($products[0][0]); // 843
BoltClock
i tried that and i got nothing...the thing is there is more then just price in the array so i would think i would need to do something like echo array_sum($products[0][0]['price']);
Matt
@John: you should have said so in your question.
BoltClock
+2  A: 

Doing some digging at the array_sum() manual (reading in the user section) I came across this function:

function array_sum_key( $arr, $index = null ){
    if(!is_array( $arr ) || sizeof( $arr ) < 1){
        return 0;
    }
    $ret = 0;
    foreach( $arr as $id => $data ){
        if( isset( $index )  ){
            $ret += (isset( $data[$index] )) ? $data[$index] : 0;
        }else{
            $ret += $data;
        }
    }
    return $ret;
}

How I would in-vision you using it, given the remarks at the manual

$sum = array_sum_key($products[0][0], 'price');

Hopefully it works out for you, as that should be an easy solution :)

Brad F Jacobs