【问题标题】:Filter JSON Output with PHP使用 PHP 过滤 JSON 输出
【发布时间】:2021-12-23 17:05:13
【问题描述】:

我一直在为这个问题挠头好几个小时,但我似乎无法让它工作。 我有一个类似这个例子的 JSON 输出:

    {
    "response": {
        "dataInfo": {
            "foundCount": 494,
            "returnedCount": 4
        },
        "data": [
            {
                "fieldData": {
                    "Closed_Date": "10/03/2021",
                    "Start_Date": "10/03/2021"
                },
                "portalData": {},
                "recordId": "152962",
                "modId": "3"
            },
            {
                "fieldData": {
                    "Closed_Date": "11/14/2021",
                    "Start_Date": "11/06/2021"
                },
                "portalData": {},
                "recordId": "153228",
                "modId": "22"
            },
            {
                "fieldData": {
                    "Closed_Date": "11/07/2021",
                    "Start_Date": "11/06/2021"
                },
                "portalData": {},
                "recordId": "153329",
                "modId": "7"
            },
            {
                "fieldData": {
                    "Closed_Date": "11/08/2021",
                    "Start_Date": "11/08/2021"
                },
                "portalData": {},
                "recordId": "153513",
                "modId": "3"
            }
        ]
    },
    "messages": [
        {
            "code": "0",
            "message": "OK"
        }
    ]
}

我想通过 Start_Date 字段使用 PHP 过滤此输出,并计算每个月创建的数量。我已经将 JSON 的结果放入一个带有 JSON 解码的数组中。

$urls = $ref->multicurlRestApi($urllinkarray, $postfield, $headers);
            
            $decode_open = json_decode($urls[0],true);

例如 this 的输出如下。

10 月:1

11 月:3

现在非常感谢任何帮助,以免我发疯

【问题讨论】:

    标签: php json filter


    【解决方案1】:
    <?php
    $jsonStr = <<<JSON
     {
        "response": {
            "dataInfo": {
                "foundCount": 494,
                "returnedCount": 4
            },
            "data": [
                {
                    "fieldData": {
                        "Closed_Date": "10/03/2021",
                        "Start_Date": "10/03/2021"
                    },
                    "portalData": {},
                    "recordId": "152962",
                    "modId": "3"
                },
                {
                    "fieldData": {
                        "Closed_Date": "11/14/2021",
                        "Start_Date": "11/06/2021"
                    },
                    "portalData": {},
                    "recordId": "153228",
                    "modId": "22"
                },
                {
                    "fieldData": {
                        "Closed_Date": "11/07/2021",
                        "Start_Date": "11/06/2021"
                    },
                    "portalData": {},
                    "recordId": "153329",
                    "modId": "7"
                },
                {
                    "fieldData": {
                        "Closed_Date": "11/08/2021",
                        "Start_Date": "11/08/2021"
                    },
                    "portalData": {},
                    "recordId": "153513",
                    "modId": "3"
                }
            ]
        },
        "messages": [
            {
                "code": "0",
                "message": "OK"
            }
        ]
    }
    JSON;
    $response = json_decode($jsonStr, true);
    if (JSON_ERROR_NONE !== json_last_error()) {
        return;
    }
    $counter = [];
    foreach ($response['response']['data'] as $item) {
        $startDate = new DateTime($item['fieldData']['Start_Date']);
        $month = $startDate->format('m');
        if (key_exists($month, $counter)) {
            $counter[$month] += 1;
        } else {
            $counter[$month] = 1;
        }
    }
    
    var_dump($counter);die;
    
    

    【讨论】:

    • 虽然您的代码可能会解决问题,但添加一些解释有助于其他人了解您所做的事情(以及原因)。
    • 谢谢你的建议,我的英语不好
    【解决方案2】:

    我首先将所有月份存储在一个键值数组中,其中键是月份,值是该月的条目数量。

    现在,遍历所有条目 ($decode_open["response"]["data"]) 并计算月份。我只是将日期转换为 UNIX 时间戳,然后使用 PHP date 函数返回来获取月份。

    然后您只需将月份添加到您的数组中(如果它不存在)并计数。

    // Your code to get the data
    $urls = $ref->multicurlRestApi($urllinkarray, $postfield, $headers);
    $decode_open = json_decode($urls[0],true);
    
    $months = []; // <- The array we store the final data in
    $items = $decode_open["response"]["data"];
    foreach($items as $item) {
        $month = date("m", strtotime($item["fieldData"]["Start_Date"]));
        if(!isset($months[$month])) $months[$month] = 0; // <- Add the month to the array if it doesn't exist
        $months[$month]++; // <- Increase the value for that month
    }
    
    print_r($months);
    

    输出应该如下:

    Array
    (
        [10] => 1
        [11] => 3
    )
    
    

    【讨论】:

    • 这太完美了!谢谢,我还有一个关于这个的快速问题。输出给了我以下Array ( [06] =&gt; 8 [07] =&gt; 6 [08] =&gt; 7 [09] =&gt; 15 [10] =&gt; 29 [11] =&gt; 5 ) 如果不是 [MONTH] => VALUE 它显示了一个静态名称,例如 [M1] => VALUE [M2] => VALUE ETC 怎么办?
    猜你喜欢
    • 1970-01-01
    • 2017-06-05
    • 2014-10-01
    • 2020-10-17
    • 2011-08-16
    • 2022-11-12
    • 2019-06-28
    • 2013-05-06
    • 1970-01-01
    相关资源
    最近更新 更多