天天看點

php對數組某一字段求和_php – 在foreach中對數組中的項目進行分組和求和

我循環周遊兩個存儲過程的結果集,根據另一個存儲過程中的字段擷取結果.

包含結果集的兩個數組是$customers和$subcustomers.

foreach($customers as $customer)

{

foreach($subcustomers as $subcustomer)

{

if($subcustomer['parent'] == $customer['id'])

{

if($customer['innumber'] == null && $subcustomer['innumber'] != null)

{

$chartInboundSub['name'] = $customer['name'];

$chartInboundSub['label'] = $subcustomer['innumber'];

$chartInboundSub['countInbound'] = $customer['count'];

$chartInboundSub['minsInbound'] = ceil($customer['duration'] / 60);

$chartInboundSub['customerid'] = $customer['id'];

array_push($out['chartInbound'], $chartInboundSub);

}

}

}

}

print_r的目前輸出($out [‘chartInbound’])如下:

Array

(

[0] => Array

(

[countInbound] => 426

[minsInbound] => 340

[name] => Telekomm

[label] => 01-02

[customerid] => 6

)

[1] => Array

(

[countInbound] => 1

[minsInbound] => 2

[name] => Telekomm

[label] => 01-02

[customerid] => 6

)

[2] => Array

(

[countInbound] => 3

[minsInbound] => 21

[name] => Telekomm

[label] => 080

[customerid] => 6

)

[3] => Array

(

[countInbound] => 1920

[minsInbound] => 15766

[name] => Telekomm

[label] => 084

[customerid] => 6

)

[4] => Array

(

[countInbound] => 2332

[minsInbound] => 17521

[name] => Telekomm

[label] => 084

[customerid] => 6

)

...

)

上述結果需要按名稱,标簽,customerid與countInbound和minsInbound求和進行分組,是以:

期望的輸出應該是:

Array

(

[0] => Array

(

[countInbound] => 427

[minsInbound] => 342

[name] => Telekomm

[label] => 01-02

[customerid] => 6

)

[1] => Array

(

[countInbound] => 3

[minsInbound] => 21

[name] => Telekomm

[label] => 080

[customerid] => 6

)

[2] => Array

(

[countInbound] => 4252

[minsInbound] => 33287

[name] => Telekomm

[label] => 084

[customerid] => 6

)

...

)