数组值总和(通过键值php)

时间:2019-09-22 09:04:06

标签: php arrays multidimensional-array

如何通过关联键检查列值?

我想通过“ 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
    )

)

所需结果:

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

5 个答案:

答案 0 :(得分:2)

这是代码段。为了获得理想的结果,您需要按tenancy_rate而不是建筑物ID进行分组,

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

)

答案 1 :(得分:1)

您可以使用foreach并按索引tenancy_rate分组

$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重新排列数组的顺序

答案 2 :(得分:1)

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

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

然后,您可以使用上一步中的数据构建结果:

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

答案 3 :(得分:1)

使用一个函数返回必要的值(费率和租金),并将这些值累加到sumArray数组中,并假设结果数组键与tenancy_rate相关(这样可以插入更多tenancy_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);

答案 4 :(得分:0)

使用此循环:

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