1
我有一個數組。 現在,它的JSON數組:用特定條件計數數組元素
{
"status":"OK",
"details":{
"Jun":[
{
"id":"8",
"order_id":"0",
"client_id":"0",
"driver_id":"3",
"status":"accept",
"order_date":"2017-06-22"
},
{
"id":"13",
"order_id":"1",
"client_id":"0",
"driver_id":"3",
"status":"accept",
"order_date":"2017-06-22"
},
{
"id":"33",
"order_id":"1",
"client_id":"0",
"driver_id":"3",
"status":"decline",
"order_date":"2017-06-22"
}
],
"Apr":[
{
"id":"7",
"order_id":"12",
"client_id":"15",
"driver_id":"3",
"status":"accept",
"order_date":"2014-04-10"
}
]
}
}
我現在的數據顯示爲每月一次。
我想每月顯示數組,但計算total accepted
,total request
,total decline
。
我的預期輸出的樣子:
{
"status":"OK",
"details":{
"Jun":[
{
"total accepted":"2",
"total decline":"1",
"total request":"3"
}
],
"Apr":[
{
"total accepted":"1",
"total decline":"0",
"total request":"1"
}
]
}
}
我目前的PHP代碼是:
while ($row = mysql_fetch_assoc($result))
{
$details_sort[] = $row;
}
$monthlyDetails = array();
foreach ($details_sort as $detail)
{
$monthName = date('M', strtotime($detail['order_date']));
if (! array_key_exists($monthName, $monthlyDetails))
{
$monthlyDetails[$monthName] = array();
}
array_push($monthlyDetails[$monthName], $detail);
}
我不明白如何計算total accepted
,total request
,total decline
。請給我一些例子。