Eli
Eli

Reputation: 1276

How to access and add array object in laravel?

$loanAmountbyMonth = Loan::select('date_release','amount_granted')
                             ->where('invalid',false)
                             ->where('transaction_year', $transyear)
                             ->get()
                             ->groupBy(function($date) {
        
           return Carbon::parse($date->date_release)->format('m'); // grouping by months
    });     

    $loanmcount2 = [];
    $loanArr2 = [];

    foreach ($loanAmountbyMonth as $key => $value) {
        $loanmcount2[(int)$key] = $value;
    }
    
    return view('src/modules-crud',compact('loanmcount2'));

Output:

string(471) "{"9":[{"date_release":"2021-09-21","amount_granted":"10000"}],"5":[{"date_release":"2021-05-23","amount_granted":"10000"},{"date_release":"2021-05-23","amount_granted":"20000"}],"6":[{"date_release":"2021-06-17","amount_granted":"10000"}],"7":[{"date_release":"2021-07-18","amount_granted":"15000"},{"date_release":"2021-07-18","amount_granted":"50000"},{"date_release":"2021-07-18","amount_granted":"14000"}],"8":[{"date_release":"2021-08-22","amount_granted":"10000"}]}"

My Desired Output:

string(61) "{"9":"10000","5":"30000","6":"10000","7":"79000","8":"10000"}"

I wanted to add the amount_granted under "7":[{"date_release":"2021-07-18","amount_granted":"15000"},{"date_release":"2021-07-18","amount_granted":"50000"},{"date_release":"2021-07-18","amount_granted":"14000"}] but I can only access the first array object value with amount granted equal to 15,000 whenever I used $value[0]['amount_granted]. If I use $value[1]['amount_granted'] it says undefined offset 1. How could I add together the amount granted in key 7? Thank you.

Upvotes: 0

Views: 166

Answers (2)

Neidi
Neidi

Reputation: 116

I don't know where and why your error happens but this way it should work.

$str = '{"9":[{"date_release":"2021-09-21","amount_granted":"10000"}],"5":[{"date_release":"2021-05-23","amount_granted":"10000"},{"date_release":"2021-05-23","amount_granted":"20000"}],"6":[{"date_release":"2021-06-17","amount_granted":"10000"}],"7":[{"date_release":"2021-07-18","amount_granted":"15000"},{"date_release":"2021-07-18","amount_granted":"50000"},{"date_release":"2021-07-18","amount_granted":"14000"}],"8":[{"date_release":"2021-08-22","amount_granted":"10000"}]}';
$arr = (array)json_decode( $str );
$result = array_map( function( $item ) {
  $sum = array_reduce( $item, function( $sum, $item ) { 
    return $sum + $item->amount_granted;
  }, 0 );
  return $sum;
}, $arr );
var_dump( $result );

Upvotes: 1

nice_dev
nice_dev

Reputation: 17825

You will need 2 nested loops for this. Loop over all amount values for a particular month and sum it up and then add it to your resultant array.

Snippet:

foreach($loanAmountbyMonth as $key => $value){
  $sum = 0;
  foreach($value as $key2 => $val){
     $sum += $val['amount_granted'];
  }
  $loanmcount2[(int)$key] = $sum;
}

Shorter version:

foreach ($loanAmountbyMonth as $key => $value) {
     $loanmcount2[(int)$key] = array_sum(array_column($value, 'amount_granted'));
}

Upvotes: 2

Related Questions