skip to Main Content

I have print_r array like this. How do I SUM field ‘credit’ ?

enter image description here

I want to result like this in html :

  1. 650000
  2. 500000
  3. 500000
  4. 500000
  5. 200000

2

Answers


  1. you can create funtion to sum your array

    function sum_credit($array){
      $sum = 0;
      foreach($array as $val){
        $sum += $val->credit;
      }
      return $sum;
    }
    

    and print your data with

    echo sum_credit($array);
    
    Login or Signup to reply.
  2. $creditSum = array_sum(array_column($array,'credit'));
    

    With array_column, a one-dimensional array is created from ‘credit’ values and then the sum is calculated with array_sum.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search