在数组下面,我必须根据[c]
索引
// $name = $array[result][0][_id][c];
我希望将有关其姓名的数据(例如:Maharashtra& Goa)与其pl值相加
Array
(
[result] => Array
(
[0] => Array
(
[_id] => Array
(
[c] => Maharashtra & Goa
[d] => MongoDate Object
(
[sec] => 1412746200
[usec] => 0
)
)
[pl] => 107369655
)
[1] => Array
(
[_id] => Array
(
[c] => unknown
[d] => MongoDate Object
(
[sec] => 1412659800
[usec] => 0
)
)
[pl] => 69356849
)
[2] => Array
(
[_id] => Array
(
[c] => Maharashtra & Goa
[d] => MongoDate Object
(
[sec] => 1412832600
[usec] => 0
)
)
[pl] => 22320916
)
[3] => Array
(
[_id] => Array
(
[c] => unknown
[d] => MongoDate Object
(
[sec] => 1412746200
[usec] => 0
)
)
[pl] => 8397870
)
[4] => Array
(
[_id] => Array
(
[c] => Bihar & Jharkhand
[d] => MongoDate Object
(
[sec] => 1412746200
[usec] => 0
)
)
[pl] => 4278330
)
[5] => Array
(
[_id] => Array
(
[c] => unknown
[d] => MongoDate Object
(
[sec] => 1412832600
[usec] => 0
)
)
[pl] => 4183450
)
[6] => Array
(
[_id] => Array
(
[c] => Tamil Nadu
[d] => MongoDate Object
(
[sec] => 1412832600
[usec] => 0
)
)
[pl] => 4113634
)
[7] => Array
(
[_id] => Array
(
[c] => Maharashtra & Goa
[d] => MongoDate Object
(
[sec] => 1412487000
[usec] => 0
)
)
[pl] => 3716731
)
[8] => Array
(
[_id] => Array
(
[c] => Maharashtra & Goa
[d] => MongoDate Object
(
[sec] => 1412659800
[usec] => 0
)
)
[pl] => 1203054
)
[9] => Array
(
[_id] => Array
(
[c] => unknown
[d] => MongoDate Object
(
[sec] => 1412573400
[usec] => 0
)
)
[pl] => 554806
)
)
[ok] => 1
)
//所需的阵列
// pl将是他们名字的总和
Array (
[0] => Array
(
[name] => Maharashtra & Goa
[pl] => 134610356
)
[1] => Array
(
[name] => unknown
[pl] => 82492975
)
[2] => Array
(
[name] => Bihar & Jharkhand
[pl] => 4278330
)
[3] => Array
(
[name] => Tamil Nadu
[pl] => 4113634
)
)
答案 0 :(得分:1)
您可以使用该名称索引对这些值进行分组,最后只需添加它们。
示例:
foreach ($array['result'] as $value) {
if(!isset($resultData[$value['_id']['c']])) { // initialize
$resultData[$value['_id']['c']] = array('name' => $value['_id']['c'], 'pl' => 0);
}
$resultData[$value['_id']['c']]['pl'] += $value['pl']; // sum them
}
$resultData = array_values($resultData); // simple reindex
echo '<pre>';
print_r($resultData);
答案 1 :(得分:0)
// i tried this
$resultData =array();
foreach($array['result'] as $key=>$val){
$itemKey = str_replace(' ', '_', $val['_id']['c']);
//$resultData[$itemKey]= $resultData[$itemKey]+$val['pl'];
if(isset($resultData[$itemKey])){
$resultData[$itemKey][] =array('name'=>$val['_id']['c'],'p1'=>$val['pl']);
$resultData[$itemKey]['total'] +=$val['pl'];
// $resultData[$itemKey][$i]['pl']+= $val['pl'];
}else{
$resultData[$itemKey][] =array('name'=>$val['_id']['c'],'p1'=>$val['pl']);
$resultData[$itemKey]['total'] =$val['pl'];
}
}
// it produce result
Array
(
[Maharashtra_&_Goa] => Array
(
[0] => Array
(
[name] => Maharashtra & Goa
[p1] => 107369655
)
[total] => 134610356
[1] => Array
(
[name] => Maharashtra & Goa
[p1] => 22320916
)
[2] => Array
(
[name] => Maharashtra & Goa
[p1] => 3716731
)
[3] => Array
(
[name] => Maharashtra & Goa
[p1] => 1203054
)
)
[unknown] => Array
(
[0] => Array
(
[name] => unknown
[p1] => 69356849
)
[total] => 82492975
[1] => Array
(
[name] => unknown
[p1] => 8397870
)
[2] => Array
(
[name] => unknown
[p1] => 4183450
)
[3] => Array
(
[name] => unknown
[p1] => 554806
)
)
[Bihar_&_Jharkhand] => Array
(
[0] => Array
(
[name] => Bihar & Jharkhand
[p1] => 4278330
)
[total] => 4278330
)
[Tamil_Nadu] => Array
(
[0] => Array
(
[name] => Tamil Nadu
[p1] => 4113634
)
[total] => 4113634
)
)
答案 2 :(得分:0)
foreach($aggregateResult['result'] as $key=>$val){
$itemKey = str_replace(' ', '_', $val['_id']['c']);
$resultData[$itemKey]= $resultData[$itemKey]+$val['pl'];
}
//will produce in key value
Array
(
[unknown] => 82492975
[Maharashtra_&_Goa] => 134610356
[Bihar_&_Jharkhand] => 4278330
[Tamil Nadu] => 4113634
)