[12] => Array
(
[type] => Other Miscellaneous Fees
[fee] => 158
[amount] => -22.56
[code] => COL_AUDIO
[feedesc] => COLLEGE AUDIO VISUAL FEE
)
[13] => Array
(
[type] => Other Miscellaneous Fees
[fee] => 158
[amount] => -297.86
[code] => COL_AUDIO
[feedesc] => COLLEGE AUDIO VISUAL FEE
)
[14] => Array
(
[type] => Other Miscellaneous Fees
[fee] => 182
[amount] => -40.00
[code] => STRAP
[feedesc] => ID STRAP
)
[15] => Array
(
[type] => Other Miscellaneous Fees
[fee] => 177
[amount] => -105.00
[code] => PRISAA
[feedesc] => PRISAA
)
所需的输出,我希望它存储在一个新的数组中。香港专业教育学院尝试使用array_sum和数组colum。但它将总结所有领域。感谢
array_sum(array_column($feesArray, 'fee'));
[0] => Array
(
[type] => Other Miscellaneous Fees
[fee] => 158
[amount] => -320.42
[code] => COL_AUDIO
[feedesc] => COLLEGE AUDIO VISUAL FEE
)
[1] => Array
(
[type] => Other Miscellaneous Fees
[fee] => 182
[amount] => -40.00
[code] => STRAP
[feedesc] => ID STRAP
)
[2] => Array
(
[type] => Other Miscellaneous Fees
[fee] => 177
[amount] => -105.00
[code] => PRISAA
[feedesc] => PRISAA
)
答案 0 :(得分:2)
要使用相同的FEE COLUMN汇总所有值,您可以创建一个for循环,迭代每个结果以检查该值是否已存在于数组中。
$data = Array(
Array
(
"type" => "Other Miscellaneous Fees",
"fee" => 158,
"amount" => -22.56,
"code" => "COL_AUDIO",
"feedesc" => "COLLEGE AUDIO VISUAL FEE"
),
Array(
"type" => "Other Miscellaneous Fees",
"fee" => 158,
"amount" => -297.86,
"code" => "COL_AUDIO",
"feedesc" => "COLLEGE AUDIO VISUAL FEE"
),
Array
(
"type" => "Other Miscellaneous Fees",
"fee" => 182,
"amount" => -40.00,
"code" => "STRAP",
"feedesc" => "ID STRAP"
),
Array
(
"type" => "Other Miscellaneous Fees",
"fee" => 177,
"amount" => -105.00,
"code" => "PRISAA",
"feedesc" => "PRISAA"
)
);
foreach ($data as $data_key => &$data_searcher) {
foreach ($data as $data_searcher_key => $data_searcher_value) {
if(
($data_searcher['fee'] === $data_searcher_value['fee']) &&
$data_key !== $data_searcher_key
){
$data_searcher['amount'] += $data_searcher_value['amount'];
unset($data[$data_searcher_key]);
}
}
}
echo "<pre>";
print_r($data);
echo "</pre>";