0

How can I check columnar values by associative key?

I want to check data by "building_id" . Here I have 2 buildings then I want rent data as sum with group by "tenancy_rate"

Input array :

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
    )

)

Desired result :

Array
(
[0] => Array
    (
        [tenancy_rate] => 0
        [rent] => 77000
    )

[1] => Array
    (
        [tenancy_rate] => 10
        [rent] => 172100
    )

[2] => Array
    (
        [tenancy_rate] => 20
        [rent] => 172100
    )

)

For this I tried PHP code

But not getting any solution

$sumArray = array();

foreach ($myArray as $k=>$subArray) {
  foreach ($subArray as $id=>$value) {
    $sumArray[$id]+=$value;
  }
}

print_r($sumArray);
2
  • 1
    Do you want to group by the building_id and sum tenancy_rate and rent ? Commented Sep 22, 2019 at 9:07
  • What you want i.e. building_id and what's your desired result i.e. tenancy_rate conflicting your expectations. Commented Sep 22, 2019 at 9:16

5 Answers 5

2

Here is the snippet. To get the desired result you need to group by tenancy_rate and not by building 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

Output:-

Array
(
    [0] => Array
        (
            [tenancy_rate] => 0
            [rent] => 77000
        )

    [1] => Array
        (
            [tenancy_rate] => 10
            [rent] => 172100
        )

    [2] => Array
        (
            [tenancy_rate] => 20
            [rent] => 172100
        )

)
Sign up to request clarification or add additional context in comments.

Comments

1

You can use foreach and group by index 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
        ];
  }
}

Working example :- https://3v4l.org/nWRGA

You can use array_values to re arrange the order of array

Comments

1

At first you need to use calculate rent sum for each tenancy rate:

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

Then you can build the result using the data from previous step:

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

Comments

1

Using a function to return the necessary values (rate and rent), and accumulating the values into the sumArray array, making the assumption that the results array keys are related to the tenancy_rate (this allows for insertion of further 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);

Comments

0

use this loop:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.