Home > front end >  Filter JSON Output with PHP
Filter JSON Output with PHP

Time:11-11

I have been scratching my head for hours on this one and I just cant seem to get it to work. I have a JSON Output like this example:

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

I want to filter this output using PHP by the Start_Date field and count how many were created in each month. I have got the results of the JSON into a array with JSON Decode.

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

For example the output of this would be as follows.

Month 10: 1

Month 11: 3

Any help is massively appreciated right now to save me going crazy

CodePudding user response:

I'd start by storing all months in a key-value-array, where the key is the month and the value to amount of entries in that month.

Now, loop through all entries ($decode_open["response"]["data"]) and calculate the month. I just converted the date to a UNIX Timestamp and then back with the PHP date function to get the month.

Then you just have to add the month to your array (if it doesn't already exist) and count it up.

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

The output should be the following:

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

CodePudding user response:

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

  • Related