fork download
  1. <?php
  2.  
  3. $json = '{
  4. "status":"OK",
  5. "details":{
  6. "Jun":[
  7. {
  8. "id":"8",
  9. "order_id":"0",
  10. "client_id":"0",
  11. "driver_id":"3",
  12. "status":"accept",
  13. "order_date":"2017-06-22"
  14. },
  15. {
  16. "id":"13",
  17. "order_id":"1",
  18. "client_id":"0",
  19. "driver_id":"3",
  20. "status":"accept",
  21. "order_date":"2017-06-22"
  22. },
  23. {
  24. "id":"33",
  25. "order_id":"1",
  26. "client_id":"0",
  27. "driver_id":"3",
  28. "status":"decline",
  29. "order_date":"2017-06-22"
  30. }
  31. ],
  32. "Apr":[
  33. {
  34. "id":"7",
  35. "order_id":"12",
  36. "client_id":"15",
  37. "driver_id":"3",
  38. "status":"accept",
  39. "order_date":"2014-04-10"
  40. }
  41. ]
  42. }
  43. }';
  44.  
  45. $array = json_decode($json, true);
  46.  
  47. $result = array();
  48. foreach ($array["details"] as $key => $value) {
  49. $accepted = array_filter($value, function($item) {
  50. return $item["status"] === "accept";
  51. });
  52.  
  53. $declined = array_filter($value, function($item) {
  54. return $item["status"] === "decline";
  55. });
  56.  
  57. $result[$key] = array(
  58. "total_accepted" => count($accepted),
  59. "total_declined" => count($declined),
  60. "total_request" => count($value)
  61. );
  62. }
  63.  
  64. var_dump($result);
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
array(2) {
  ["Jun"]=>
  array(3) {
    ["total_accepted"]=>
    int(2)
    ["total_declined"]=>
    int(1)
    ["total_request"]=>
    int(3)
  }
  ["Apr"]=>
  array(3) {
    ["total_accepted"]=>
    int(1)
    ["total_declined"]=>
    int(0)
    ["total_request"]=>
    int(1)
  }
}