1

I have an array. Now its a json array:

{
  "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"
      }
    ]
  }
}

My data show as monthly now.

I want to show array monthly but calculate total accepted, total request, total decline.

My expected Output look like:

{
  "status":"OK",
  "details":{
    "Jun":[
      {
        "total accepted":"2",
        "total decline":"1",
        "total request":"3"
      }
    ],
    "Apr":[
      {
        "total accepted":"1",
        "total decline":"0",
        "total request":"1"
      }
    ]
  }
}

My Current PHP CODE is:

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);             
}

I can not understand how to calculate total accepted, total request, total decline. Please give me some example.

1 Answer 1

3

You can use array_filter like;

<?php

$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"
            }
        ]
    }
}';

$array = json_decode(json, true);

$result = array();
foreach ($array["details"] as $key => $value) {
    $accepted = array_filter($value, function($item) {
        return $item["status"] === "accept";
    });

    $declined = array_filter($value, function($item) {
        return $item["status"] === "decline";
    }); 

    $result[$key] = array(
        "total_accepted" => count($accepted),
        "total_declined" => count($declined),
        "total_request"  => count($value)
    );
}

var_dump($result);

You can see demo here: Demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.