繁体   English   中英

计算具有特定条件的数组元素

[英]count array element with specific condition

我有一个数组。 现在,它是一个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 请举个例子。

你可以像这样使用array_filter ;

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

您可以在此处查看演示: 演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM