2 回答

TA贡献1796条经验 获得超4个赞
这是问题的解决方案。
<?php
$sample = array(
"term_1_mid" => array(
"English" => 56,
"Mathematics" => 34,
"Creative Arts" => 87),
"terrm_1_end" => array(
"English" => 67,
"Mathematics" => 59,
"Creative Arts" => 95)
);
# Initializing array to store the result
$output_array = array();
# Loop for adding the values
foreach($sample as $sample_key => $sample_value){
foreach ($sample_value as $key => $value){
$output_array[$key] += $value;
}
}
# To check the data in array
foreach($output_array as $key => $value){
# used br tag only to show the each values in each line
echo $key . " => ". $value . "<br>";
}
?>
输出 :
English => 123
Mathematics => 93
Creative Arts => 182

TA贡献1799条经验 获得超6个赞
这是一个演示。
$sample = array(
"term_1_mid" => array(
"English" => 56,
"Mathematics" => 34,
"Creative Arts" => 87),
"terrm_1_end" => array(
"English" => 67,
"Mathematics" => 59,
"Creative Arts" => 95)
);
$arrSum =[];
foreach($sample as $term=>$termname){
foreach($termname as $sub=>$mark){
if(!isset($arrSum[$sub])){$arrSum[$sub] = 0;}
$arrSum[$sub] += $mark;
}
}
print_r($arrSum);
- 2 回答
- 0 关注
- 93 浏览
添加回答
举报