Php json parsing objects

I have a JSON file that I need to parse. I am new to php and am trying to figure out how to parse an object correctly. My question is:How do I parse the objects of an object that have different names

One object has a name Thresh

, the next object in the list has a name Aatrox

, the name of the top-level objectdata

This is what the JSON looks like. I can access the information if I know the name of the object $champion = $jfo->data->Thresh;

, but I don't want to enter all the champion names. Is there an easy way to get all the individual objects without knowing the names?Maybe regex?

"data": {
    "Thresh": {
        "id": 412,
        "key": "Thresh",
        "name": "Thresh",
        "title": "the Chain Warden",
        "image": {
            "full": "Thresh.png",
            "sprite": "champion3.png",
            "group": "champion",
            "x": 336,
            "y": 0,
            "w": 48,
            "h": 48
        },

 "Aatrox": {
        "id": 266,
        "key": "Aatrox",
        "name": "Aatrox",
        "title": "the Darkin Blade",
        "image": {
            "full": "Aatrox.png",
            "sprite": "champion0.png",
            "group": "champion",
            "x": 0,
            "y": 0,
            "w": 48,
            "h": 48
        },

      

+3


source to share


1 answer


If you want to go through every champion, I would recommend using a foreach loop in PHP. You can use it as such:



foreach($json->data as $champion)
{
    // Do something
}

      

+4


source







All Articles