阵列值总和按键值php

问题描述 投票:0回答:5

如何通过关联密钥检查柱状值?

我想通过“ building_id”检查数据。在这里我有两座建筑物 然后,我希望通过“ 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 ) )
the nessed结果:

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);

这里是片段。为了获得所需的结果,您需要按penancy_rate进行分组,而不是通过构建ID,
php arrays multidimensional-array sum grouping
5个回答
2
投票
$result = []; foreach ($arr as $val) { // as I see, rent for some array not there, so setting it to 0 $val['rent'] = ($val['rent'] ?? 0); if (isset($result[$val['tenancy_rate']]['rent'])) { $result[$val['tenancy_rate']]['rent'] += $val['rent']; } else { $result[$val['tenancy_rate']] = [ 'tenancy_rate' => $val['tenancy_rate'], 'rent' => $val['rent']]; } } print_r($result);

demo

输出: -

Array ( [0] => Array ( [tenancy_rate] => 0 [rent] => 77000 ) [1] => Array ( [tenancy_rate] => 10 [rent] => 172100 ) [2] => Array ( [tenancy_rate] => 20 [rent] => 172100 ) )

您可以通过索引使用
foreach
和组

1
投票

$f = [];
foreach($a as $v){
  if(!empty($f[$v['tenancy_rate']])){
    $f[$v['tenancy_rate']]['rent'] += $v['rent'];
  }else{
    $f[$v['tenancy_rate']] = [
            'tenancy_rate' => $v['tenancy_rate'],
            'rent'         => isset($v['rent']) ? $v['rent'] : 0
        ];
  }
}
工程示例:-
Https://3v4l.org/nwrga

您可以使用

array_values重新安排阵列的顺序

首先,您需要使用每个租赁费率计算租金总和:

$rentSums = [];
foreach ($input as $info) {
    $tenancyRate = $info['tenancy_rate'] ?? 0;
    $rent = $info['rent'] ?? 0;
    $rentSum = $rentSums[$tenancyRate] ?? 0;
    $rentSums[$tenancyRate] = $rentSum + $rent;
}


1
投票

$result = []; foreach ($rentSums as $tenancyRate => $rentSum) { $result[] = [ 'tenancy_rate' => $tenancyRate, 'rent' => $rentSum, ]; }


通过函数返回必要的值(利率和租金),并将值累积到

sumArray
数组中,从而假设结果数组键与thenancy_rate相关(这允许插入其他penancy_rates)。

$sumArray = []; function processBuildingInfo($building) { return [ $building['tenancy_rate']??0, $building['rent']??0 ]; } foreach ($myArray as $k=>$subArray) { list($rate, $rent) = processBuildingInfo($subArray); $sumArray[$rate/10] = [ 'tenancy_rate' => $rate, 'rent' => $sumArray[$rate/10]['rent'] + $rent ]; } print_r($sumArray);

1
投票

使用此循环:

foreach ($myArray as $k=>$subArray) { foreach ($subArray as $id=>$valueArray) { $sumArray[$id]+=[ 'tenancy_rate' => $valueArray['tenancy_rate'], 'rent' => $valueArray['rent'] ]; } }
    

0
投票
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.