Why Increment not Effect in Collection Cakephp 3?

$totalakok = 0;
$data = $datadupak['data'];                                                                 
$koleksi = new Collection($data);
$jumlahtotalangkakredit = $koleksi->each(function ($value, $key) use ($totalakok){
    $kolekkomponenjad = Collection($value->tblkomponenjad);
    $kolekkomponenjad->each(function ($v, $k)use ($totalakok) {
          $kolektblsubkomponenjad = Collection($v->tblsubkomponenjad);
          $kolektblsubkomponenjad->each(function ($va, $ka)use ($totalakok) {
              $kolektblusulandupak = Collection($va->tblusulandupakjad);
                $kolektblusulandupak->each(function ($vas, $kas) use ($totalakok) {
                    $totalok = $vas->total_angka_kredit;
                    $totalakok += $totalok;
                });
          });
         
    });
   
});
return debug($totalakok);

Why $totalakok had value 0 ???

Put a debug inside your innermost each loop and see if it’s ever getting there? Without seeing your actual data structure, I have to wonder if you’ve got an issue somewhere with thinking something is an array when it’s not.

You never return the calculated value of $totalakok up through the nest of functions. Nor do you take the alternative course of passing it into the anonymous functions as a reference (...->each(function ($v, $k) use (&$totalakok)).

Without doing one of these things I think 0 would be expected.

A third way to capture your value is to accumulate it in a class property. Even in a deep anonymous function like your’s $this is available in the function ($this->totalakok += $totalok;)