我正在阅读一份打印出来的文件。
{
"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"
}
]
}
是否可以在单独的php文件中阅读此内容并计算总收入?
即2500 + 1500 = 4000
答案 0 :(得分:3)
试试这个:
$decoded = json_decode($json, true); // Decode JSON to array
echo array_sum(array_column($decoded['data'], 'earnings')); // Calculate sum of 'earnings' index within 'data'