我正在使用一个返回JSON对象的API,然后使用
将其解码为数组我需要计算数组中所有[data]
项,然后生成每个
[Total]
值的总和
这是我的数组: -
Array
(
[errorCode] => 0
[result] => OK
[data] => Array
(
[0] => Array
(
[fieldData] => Array
(
[Due Date] => 11/30/2017
[Date] => 11/30/2017
[Total] => 128.97
[Customers::Company] => A B C Lock & Key
[Status] => Paid
[Date Payment] => 11/30/2017
)
[portalData] => Array
(
)
[recordId] => 1
[modId] => 4
)
[1] => Array
(
[fieldData] => Array
(
[Due Date] => 12/01/2017
[Date] => 12/01/2017
[Total] => 256
[Customers::Company] => Kim Peacock Beringhause
[Status] => Paid
[Date Payment] => 12/01/2017
)
[portalData] => Array
(
)
[recordId] => 2
[modId] => 3
)
[2] => Array
(
[fieldData] => Array
(
[Due Date] => 11/30/2017
[Date] => 11/30/2017
[Total] => 1880
[Customers::Company] => Norton, Robert L Esq
[Status] => Unpaid Overdue
[Date Payment] =>
)
[portalData] => Array
(
)
[recordId] => 3
[modId] => 0
)
[3] => Array
(
[fieldData] => Array
(
[Due Date] => 12/22/2017
[Date] => 12/22/2017
[Total] => 1278
[Customers::Company] => Shapiro, Mark R Esq
[Status] => Unpaid
[Date Payment] =>
)
[portalData] => Array
(
)
[recordId] => 10
[modId] => 1
)
)
)
我试过了:
count($array)
返回3
我可以看到的是该父数组中的3个项目的数量(errorCode,结果和数据),但我无法弄清楚如何关注data
仅
我之后的计数值为4
,总值为3542.97
。
答案 0 :(得分:1)
如需计数,您需要: -
count($array['data']);
获取所有Total
索引值的总和: -
echo array_sum(array_column(array_column($array['data'],'fieldData'),'Total'));
合并输出: - https://eval.in/913897