0
$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.

2 Answers 2

2

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'));
}
Sign up to request clarification or add additional context in comments.

2 Comments

array_column() expects parameter 1 to be array, object given
@Eli Deriving from the JSON you shared, it works fine sandbox.onlinephpfunctions.com/code/…
1

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

2 Comments

syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ',' or ')'
Just changed it to work with PHP below 7.4, thanks

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.