如何通过关联键检查列值?
我想通过 "building_id" 检查数据。在这里,我有 2 座建筑物,然后我想将租金数据作为“tenancy_rate”分组的总和
输入数组:
Array
(
[0] => Array
(
[id] => 34
[building_id] => 786
[tenancy_rate] => 0
[rent_per_room] => 10000
[management_fee_per_room] => 0
)
[1] => Array
(
[id] => 35
[building_id] => 786
[tenancy_rate] => 10
[rent_per_room] => 11810
[management_fee_per_room] => 5400
[rent] => 86050
)
[2] => Array
(
[id] => 36
[building_id] => 786
[tenancy_rate] => 20
[rent_per_room] => 11810
[management_fee_per_room] => 5400
[rent] => 86050
)
[3] => Array
(
[id] => 56
[building_id] => 798
[tenancy_rate] => 0
[rent_per_room] => 10000
[management_fee_per_room] => 5400
[rent] => 77000
)
[4] => Array
(
[id] => 57
[building_id] => 798
[tenancy_rate] => 10
[rent_per_room] => 11810
[management_fee_per_room] => 5400
[rent] => 86050
)
[5] => Array
(
[id] => 58
[building_id] => 798
[tenancy_rate] => 20
[rent_per_room] => 11810
[management_fee_per_room] => 5400
[rent] => 86050
)
)
期望的结果:
Array
(
[0] => Array
(
[tenancy_rate] => 0
[rent] => 77000
)
[1] => Array
(
[tenancy_rate] => 10
[rent] => 172100
)
[2] => Array
(
[tenancy_rate] => 20
[rent] => 172100
)
)
为此,我尝试了 PHP 代码
但没有得到任何解决方案
$sumArray = array();
foreach ($myArray as $k=>$subArray) {
foreach ($subArray as $id=>$value) {
$sumArray[$id]+=$value;
}
}
print_r($sumArray);
泛舟湖上清波郎朗
白板的微信
弑天下
德玛西亚99
随时随地看视频慕课网APP