0

我有这个数组

[2] => Array
        (
            [0] => 1160
            [1] => 1155
            [2] => 06/26/2013
            [3] => 32.99
        )

    [3] => Array
        (
            [0] => 1160
            [1] => 1155
            [2] => 06/26/2013
            [3] => 0.00
        )

    [4] => Array
        (
            [0] => 1160
            [1] => 1155
            [2] => 06/24/2013
            [3] => 20.99
        )

    [5] => Array
        (
            [0] => 1160
            [1] => 1155
            [2] => 06/24/2013
            [3] => 10.78
        )

问题::我想基于“oder_id”数组[0]相同的值我想对“金额”数组[3]求和

帮我解决这个问题

4

2 回答 2

2
$results = array();

foreach($data as $val){
     if(!array_key_exists($val[0], $results)){
         $results[$val[0]] = 0;
     }
     $results[$val[0]]+= $val[3];
}

在此之后 $results 将包含从 order_id 中键入的总数

于 2013-07-02T06:00:40.837 回答
2

你可以foreach这样使用

$sum = 0;
foreach ($rows as $row) {
    $sum += $row[0];
}
于 2013-07-02T06:01:33.027 回答