-1

Let say I have a function inside a controller of a CI

    public function mr_printing () {

    $code = $_POST ['item_id'] ;
    $count = count($code) ;
    for ($i=0; $i < $count; $i++) {
      $idline = $code [$i] ;
      $new ['item'] = $this -> model_prcsys -> get_line_data (md5(base64_decode($idline))) ;
      $arr = $this -> model_prcsys -> get_sum_received ($new ['item'] ['pr_line_id']) ;
      $sum = ($arr) ;
      print_r($sum);echo "<br><br>";
    }
}

by that, I have output :

Array ( [0] => Array ( [qty_incoming] => 5 ) [1] => Array ( [qty_incoming] => 2 ) )

Array ( [0] => Array ( [qty_incoming] => 3 ) ) 

From the first array, I need to get value from SUM for every array. In another word, I need output like this:

7

3

Anyone, please?

2

3 Answers 3

3

Try this

echo array_sum(array_column($arr, 'qty_incoming'));
Sign up to request clarification or add additional context in comments.

4 Comments

$arr = $this -> model_prcsys -> get_sum_received ($new ['item'] ['pr_line_id']) ; $sum = array_sum($arr) ; print_r($arr);echo "<br><br>"; print_r($sum);echo "<br><br>"; have value: Array ( [0] => Array ( [qty_incoming] => 5 ) [1] => Array ( [qty_incoming] => 2 ) ) 0 Array ( [0] => Array ( [qty_incoming] => 3 ) ) 0
this will give 0 ans
Thanks @ user1234
Thanks, Rakesh .. It saved my day ... :) never thought array_column
1

1)fetch the particular column from multidimentional array using array_colum.

2)sum using array_sum.

$total =  array_sum(array_column($array_values, 'qty_incoming'));
echo $total;

Comments

0

Try this

$sum = 0;
foreach ($arr as $item) {
    $sum += $item['qty_incoming'];
}
echo $sum;

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.