0

I am confused by the addition of array values, here I do not add all of them directly, because these values will be grouped according to the Bimbel _code and code criteria, then after that will be added up.

I've array like this

    array(3) {
  [0]=>
  array(5) {
    [0]=>
    object(stdClass)#85 (3) {
      ["bimbel_kode"]=>
      string(2) "P1"
      ["kriteria_kode"]=>
      string(2) "C2"
      ["nilai"]=>
      string(1) "2"
    }
    [1]=>
    object(stdClass)#86 (3) {
      ["bimbel_kode"]=>
      string(2) "P1"
      ["kriteria_kode"]=>
      string(2) "C2"
      ["nilai"]=>
      string(4) "1.25"
    }
    [2]=>
    object(stdClass)#87 (3) {
      ["bimbel_kode"]=>
      string(2) "P1"
      ["kriteria_kode"]=>
      string(2) "C2"
      ["nilai"]=>
      string(3) "2.5"
    }
    [3]=>
    object(stdClass)#88 (3) {
      ["bimbel_kode"]=>
      string(2) "P1"
      ["kriteria_kode"]=>
      string(2) "C2"
      ["nilai"]=>
      string(4) "3.75"
    }
    [4]=>
    object(stdClass)#89 (3) {
      ["bimbel_kode"]=>
      string(2) "P1"
      ["kriteria_kode"]=>
      string(2) "C2"
      ["nilai"]=>
      string(3) "1.5"
    }
  }
  [1]=>
  array(5) {
    [0]=>
    object(stdClass)#99 (3) {
      ["bimbel_kode"]=>
      string(2) "P2"
      ["kriteria_kode"]=>
      string(2) "C2"
      ["nilai"]=>
      string(1) "2"
    }
    [1]=>
    object(stdClass)#100 (3) {
      ["bimbel_kode"]=>
      string(2) "P2"
      ["kriteria_kode"]=>
      string(2) "C2"
      ["nilai"]=>
      string(4) "1.25"
    }
    [2]=>
    object(stdClass)#101 (3) {
      ["bimbel_kode"]=>
      string(2) "P2"
      ["kriteria_kode"]=>
      string(2) "C2"
      ["nilai"]=>
      string(3) "0.5"
    }
    [3]=>
    object(stdClass)#102 (3) {
      ["bimbel_kode"]=>
      string(2) "P2"
      ["kriteria_kode"]=>
      string(2) "C2"
      ["nilai"]=>
      string(4) "2.25"
    }
    [4]=>
    object(stdClass)#103 (3) {
      ["bimbel_kode"]=>
      string(2) "P2"
      ["kriteria_kode"]=>
      string(2) "C2"
      ["nilai"]=>
      string(3) "2.5"
    }
  }
  [2]=>
  array(5) {
    [0]=>
    object(stdClass)#113 (3) {
      ["bimbel_kode"]=>
      string(2) "P3"
      ["kriteria_kode"]=>
      string(2) "C2"
      ["nilai"]=>
      string(3) "1.5"
    }
    [1]=>
    object(stdClass)#114 (3) {
      ["bimbel_kode"]=>
      string(2) "P3"
      ["kriteria_kode"]=>
      string(2) "C2"
      ["nilai"]=>
      string(4) "1.25"
    }
    [2]=>
    object(stdClass)#115 (3) {
      ["bimbel_kode"]=>
      string(2) "P3"
      ["kriteria_kode"]=>
      string(2) "C2"
      ["nilai"]=>
      string(3) "2.5"
    }
    [3]=>
    object(stdClass)#116 (3) {
      ["bimbel_kode"]=>
      string(2) "P3"
      ["kriteria_kode"]=>
      string(2) "C2"
      ["nilai"]=>
      string(4) "3.75"
    }
    [4]=>
    object(stdClass)#117 (3) {
      ["bimbel_kode"]=>
      string(2) "P3"
      ["kriteria_kode"]=>
      string(2) "C2"
      ["nilai"]=>
      string(3) "2.5"
    }
  }
}

and I want to get the reults like this

array(3) {
  [0]=>
  array(5) {
    [0]=>
    array(3) {
      ["bimbel_kode"]=>
      string(2) "P1"
      ["kriteria_kode"]=>
      string(2) "C2"
      ["nilai"]=>
      float(11)
    }
  }
  [1]=>
  array(5) {
    [0]=>
    array(3) {
      ["bimbel_kode"]=>
      string(2) "P2"
      ["kriteria_kode"]=>
      string(2) "C2"
      ["nilai"]=>
      float(8.5)
    }
  }
  [2]=>
  array(5) {
    [0]=>
    array(3) {
      ["bimbel_kode"]=>
      string(2) "P3"
      ["kriteria_kode"]=>
      string(2) "C2"
      ["nilai"]=>
      float(11.5)
    }
  }
}

in other words I want to add values based on bimbel_kode & criteria_kode can someone help me? I only need a total for each array based on the Bimbel_code & code_criteria

I've used this for run my code

foreach($arsubkrit as $num => $values) {
    $sum += $values->nilai;
}

following this tricks link but I got an error

and I try to use this

     foreach($arsubkrit as $value) {
            foreach($value as $key => $number) {
                (!isset($res[$key])) ?
                $res[$key] = $number->nilai :
                $res[$key] += $number->nilai;
            }
        }

following this references link

and this is the results when I use that code

Array ( [0] => 5.5 [1] => 3.75 [2] => 5.5 [3] => 9.75 [4] => 6.5 )

very far from the results I expected

AdityaDees
  • 1,022
  • 18
  • 39

1 Answers1

1

now I have found a solution for my question. just combine array_sum and array_column like this

 $nilai_sub = [];
     foreach ($arsubkrit as $key => $value) {
         $nilai_sub[] = [
             'bimbel_kode' => $value[$key]->bimbel_kode,
             'kriteria_kode' => $value[$key]->kriteria_kode,
             'nilai' => array_sum(array_column($arsubkrit[$key],'nilai'))
         ];
     }

now, the results are the same as I expected.

image

AdityaDees
  • 1,022
  • 18
  • 39