-1

I am reading form a a file that print out of the following.

    {  
   "draw":0,
   "recordsTotal":3995,
   "recordsFiltered":1,
   "data":[  
      {  
         "customer_id":"1",
         "channel":"2",
         "date":"2017-03\/21 ",
         "earnings":"2500"
      },
      {  
         "customer_id":"2",
         "channel":"2",
         "date":"2017-03\/21 ",
         "earnings":"1500"
      }
   ]
}

is it possible in a separate php file to read this and to calcualte the total earnings?

i.e 2500 + 1500 = 4000

Nawin
  • 1,653
  • 2
  • 14
  • 23
marvillous
  • 131
  • 1
  • 7

1 Answers1

3

Try this:

$decoded = json_decode($json, true); // Decode JSON to array

echo array_sum(array_column($decoded['data'], 'earnings')); // Calculate sum of 'earnings' index within 'data'
Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32