简体   繁体   中英

PHP compare totals from multi-dimensional array

The array presented below has values for three consecutive dates. I am trying to calculate the difference between the total of the values from the first date, and the total of the values from the second date, and the total of the values from the third date.

Any suggestions on how best to accomplish this? Thanks.

Array
(
[Nov 18, 2011] => Array
    (
        [C] => Array
            (
                [C] => Array
                    (
                        [T] => 50803.805765535
                    )
                [S] => Array
                    (
                        [T] => 32908.863528
                    )
            )
    )
[Dec 11, 2011] => Array
    (
        [C] => Array
            (
                [C] => Array
                    (
                        [T] => 31746.502038235
                    )
                [S] => Array
                    (
                        [T] => 16836.613004414
                    )
            )
      )
[Dec 24, 2011] => Array
    (
        [C] => Array
            (
                [C] => Array
                    (
                        [T] => 43524.786543288
                    )
                [S] => Array
                    (
                        [T] => 15722.772389011
                    )
            )
      )
)

If your array is named $arr:

$totals = array();

foreach ($arr as $key => $val){

  $totals[$key] = $val['C']['C']['T'] + $val['C']['S']['T'];
}

print_r($totals);
exit;

I'm not sure I understand your problem 100% correctly, but here's a try. If the array is $a, array_value_recursive() function will extract just the values of 'T' for each date, then echo the difference between the two. The output looks like:

Nov 18, 2011 (C 50803.805766 / S 32908.863528) - Dec 11, 2011 (C 31746.502038 / S 16836.613004) = C 19057.303727 / S 16072.250524
Dec 11, 2011 (C 32908.863528 / S 31746.502038) - Dec 24, 2011 (C 16836.613004 / S 43524.786543) = C 16072.250524 / S -11778.284505

The code is:

$keys = array_keys($a);
$values = array_value_recursive('T',$a);

for($i=0;$i<count($keys);$i++) {
    if (isset($keys[$i+$i])) {
        printf( "%s (C %f / S %f) - %s (C %f / S %f) = C %f / S %f\n",
            $keys[$i], $values[$i+$i], $values[$i+$i+1],
            $keys[$i+1], $values[$i+$i+2], $values[$i+$i+3],
            $values[$i+$i] - $values[$i+$i+2],
            $values[$i+$i+1] - $values[$i+$i+3] 
            );
    }
}

function array_value_recursive($key, array $arr){
    $val = array();
    array_walk_recursive($arr, function($v, $k) use($key, &$val){
        if($k == $key) array_push($val, $v);
    });
    return count($val) > 1 ? $val : array_pop($val);
}

edit : updated the loop and values after correcting

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM