为了账号安全,请及时绑定邮箱和手机立即绑定

数组值和键值php

数组值和键值php

PHP
泛舟湖上清波郎朗 2022-01-24 09:10:48
如何通过关联键检查列值?我想通过 "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);
查看完整描述

3 回答

?
白板的微信

TA贡献1883条经验 获得超3个赞

这是片段。要获得所需的结果,您需要按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);

演示


输出:-


Array

(

    [0] => Array

        (

            [tenancy_rate] => 0

            [rent] => 77000

        )


    [1] => Array

        (

            [tenancy_rate] => 10

            [rent] => 172100

        )


    [2] => Array

        (

            [tenancy_rate] => 20

            [rent] => 172100

        )


)


查看完整回答
反对 回复 2022-01-24
?
弑天下

TA贡献1818条经验 获得超8个赞

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


查看完整回答
反对 回复 2022-01-24
?
德玛西亚99

TA贡献1770条经验 获得超3个赞

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


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

    ];

}


查看完整回答
反对 回复 2022-01-24
  • 3 回答
  • 0 关注
  • 226 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号