mercredi 23 mai 2018

How to get or split some specific json value using PHP

I managed to used GuzzleHttp\Client and json_decode in order to retrieve the JSON responses from an API.

See results below:

    [{
        "INDEX": "1",
        "DESCRIPTION": "1; 0; US; United States of America;"
    },
    {
        "INDEX": "2",
        "DESCRIPTION": "1; 0; CN; China;"
    },
    {
        "INDEX": "3",
        "DESCRIPTION": "1; 0; MY; Malaysia;"
    },
    {
        "INDEX": "4",
        "DESCRIPTION": "1; 0; JP; Japan;"
    },
    {
        "INDEX": "5",
        "DESCRIPTION": "1; 0; MX; Mexico;"
    },
    {
        "INDEX": "6",
        "DESCRIPTION": "1; 0; SG; Singapore;"
    }]


But now, I only want to get some of the values in the "DESCRIPTION" inorder to save in a logfile but I don't know how split it. Can someone help me how to split it using foreach loop or for loop? Or any method to get the result?

I just want it to be like this:

[
{
    "CATEGORY" : "COUNTRIES",
    "LISTS" : [
        {
            "NAME": "United States of America",
            "CODE": "US"
        },
        {
            "NAME": "China",
            "CODE": "CN"
        },
        {
            "NAME": "Malaysia",
            "CODE": "MY"
        },
        {
            "NAME": "Japan",
            "CODE": "JP"
        },
        {
            "NAME": "Mexico",
            "CODE": "MX"
        },
        {
            "NAME": "Singapore",
            "CODE": "SG"
        }
    ]
}
]



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire