2 回答

TA貢獻(xiàn)1796條經(jīng)驗 獲得超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貢獻(xiàn)1799條經(jīng)驗 獲得超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 關(guān)注
- 109 瀏覽
添加回答
舉報