我必须总结多维数组的数组键,如下所示:
Array
(
[0] => Array
(
[qty_of_leads_by_levels] => Array
(
[0] => 6054
[1] => 454
[2] => 113
[3] => 85
[4] => 42
[5] => 21
[6] => 5
[7] => 1
[8] => 1
)
[avg_conv_cof_arry] => Array
(
[0] => 0.08
[1] => 0.1
[2] => 0.34
[3] => 0.25
[4] => 0.28
[5] => 0.15
[6] => 0.16
[7] => 0.49
[8] => 0.52
)
[avg_deal_size] => 54545
[total_bgt] => 143763
)
[1] => Array
(
[qty_of_leads_by_levels] => Array
(
[0] => 11393
[1] => 8144
[2] => 6490
[3] => 4868
[4] => 2434
[5] => 1217
[6] => 305
[7] => 76
[8] => 57
)
[avg_conv_cof_arry] => Array
(
[0] => 0.41
[1] => 0.42
[2] => 0.68
[3] => 0.5
[4] => 0.55
[5] => 0.3
[6] => 0.31
[7] => 0.98
[8] => 1.01
)
[avg_deal_size] => 54545
[total_bgt] => 297490245
)
[2] => Array
(
[qty_of_leads_by_levels] => Array
(
[0] => 366
[1] => 366
[2] => 366
[3] => 366
[4] => 366
[5] => 184
[6] => 46
[7] => 11
[8] => 9
)
[avg_conv_cof_arry] => Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[4] => 0.5
[5] => 0.25
[6] => 0.25
[7] => 0.75
[8] => 0.75
)
[avg_deal_size] => 54545
[total_bgt] => 1981167
)
)
在这里,我想获得数组键的总和,例如,键[qty_of_leads_by_levels]的值应该与相同的键和输出数组相加:
[qty_of_leads_by_levels] => Array
(
[0] => 17814.49 //sum
[1] => 8965.52
[2] => 6971.02
[3] => 5320.75
[4] => 2843.33
[5] => 1422.7
[6] => 356.72
[7] => 90.22
[8] => 69.28
)
[avg_conv_cof_arry] => Array
(
[0] => //sum
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
)
[avg_deal_size] => //sum
[total_bgt] => //sum
答案 0 :(得分:2)
首先将您的关联数组分配给$ inputArray。
$sumArr = array();
foreach($inputArray as $key1=>$value1){
foreach($value1 as $key2=>$value2){
if(is_array($value2)){
foreach($value2 as $key3=>$value3){
if(isset($sumArr[$key3])) {
$sumArr[$key3] = $sumArr[$key3] + $value3;
} else {
$sumArr[$key3] = $value3;
}
}
}
}
}
print_r($sumArr);
答案 1 :(得分:1)
你可以通过循环数组来完成:
$qty_of_leads_by_levels = array();
foreach ($your_arr as $key_internal=>$arr_internal){
foreach ($arr_internalas $key_internal_2=>$value_internal_2){
foreach ($value_internal_2 as $key=>$value){
if(isset($qty_of_leads_by_levels[$key])) {
$qty_of_leads_by_levels[$key] = $qty_of_leads_by_levels[$key] + $value
} else {
$qty_of_leads_by_levels[$key] = $value;
}
}
}
}
print_r($qty_of_leads_by_levels);
答案 2 :(得分:1)
您可以使用array_walk_recursive
使用传递引用&$final
来执行此操作
对于每个item
递归。
$final = array();
array_walk_recursive( $array, function ( $item, $key ) use ( &$final ) {
$final[ $key ] = isset( $final[ $key ] ) ? $item + $final[ $key ] : $item;
} );
unset( $final['avg_deal_size'], $final['total_bgt'] );
echo '<pre>';print_r($final);echo '</pre>';
请参见最后的演示输出here
输出:
<pre>Array
(
[0] => 17814.49
[1] => 8965.52
[2] => 6971.02
[3] => 5320.75
[4] => 2843.33
[5] => 1422.7
[6] => 356.72
[7] => 90.22
[8] => 69.28
)
</pre>
答案 3 :(得分:1)
我用RecursiveArrayIterator
尝试维护所有密钥。看看下面的解决方案:
$array = array
(
array
(
'qty_of_leads_by_levels' => array
(
0 => 6054,
1 => 454,
),
'avg_conv_cof_arry' => array
(
0 => 0.08,
1 => 0.1
),
'avg_deal_size' => 1,
'total_bgt' => 1
),
array
(
'qty_of_leads_by_levels' => array
(
0 => 11393,
1 => 8144,
),
'avg_conv_cof_arry' => array
(
0 => 0.41,
1 => 0.42
),
'avg_deal_size' => 2,
'total_bgt' => 2
),
array
(
'qty_of_leads_by_levels' => array
(
0 => 366,
1 => 366
),
'avg_conv_cof_arry' => array
(
0 => 1,
1 => 1
),
'avg_deal_size' => 3,
'total_bgt' => 3
)
);
$new_array = array();
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
foreach ($iterator as $key => $value) {
$keys = array();
$keys[] = $key;
for ($i = $iterator->getDepth() - 1; $i >= 0; $i--) {
$keys[] = $iterator->getSubIterator($i)->key();
}
//get key path
$key_paths = array_reverse($keys);
if((count($key_paths) > 2)) {
if(!isset($new_array[$key_paths[1]][$key])){
$new_array[$key_paths[1]][$key] = 0;
}
$new_array[$key_paths[1]][$key] += $value;
}
else {
if(!isset($new_array[$key_paths[1]])){
$new_array[$key_paths[1]] = 0;
}
$new_array[$key_paths[1]] += $value;
}
}
print_r($new_array);
<强>输出强>
Array
(
[qty_of_leads_by_levels] => Array
(
[0] => 17813
[1] => 8964
)
[avg_conv_cof_arry] => Array
(
[0] => 1.49
[1] => 1.52
)
[avg_deal_size] => 6
[total_bgt] => 6
)
答案 4 :(得分:0)
<?php
$sumArray = [];
foreach($array as $r)
for($i = 0; $i < count($r); $i++)
$sumArray[$i] += $r['qty_of_leads_by_levels'][$i];
?>
答案 5 :(得分:0)
以下适用于PHP&gt; 4
$arr = array_column($input, 'qty_of_leads_by_levels');
$sumArray = array();
array_walk($arr, function ($val, $key) use ($sumArray){
foreach($val as $k => $v) {
$sumArray[$k] = isset($sumArray[$k]) ? $sumArray[$k]+$v : $v;
}
});
print_r($sumArray);
输出:
Array
(
[0] => 17814.49
[1] => 8965.52
[2] => 6971.02
[3] => 5320.75
[4] => 2843.33
[5] => 1422.7
[6] => 356.72
[7] => 90.22
[8] => 69.28
)
答案 6 :(得分:0)
我通过以下方式尝试了这个:
SqlCommand cmd = new SqlCommand(
"SP_LastRead", conn);
DataTable dtData = new DataTable();
cmd.CommandType = CommandType.StoredProcedure;
for (int i = 0; i < query3.Count(); i++)
{
cmd.Parameters.Add("@NoWater", SqlDbType.VarChar).Value = query3.Select(m=>m.SharingNo).ToString();
}
rdr = cmd.ExecuteReader();
dtData.Load(rdr);