k3vy w3vy
k3vy w3vy

Reputation: 131

php json parsing objects

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

One object has the name Thresh the next object in the list has the name Aatrox the name of the top level object is data

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 have to type in all the names of the champions. Is there an easy way to obtain all the separate 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
        },

Upvotes: 3

Views: 62

Answers (1)

Brian
Brian

Reputation: 1667

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

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

Upvotes: 4

Related Questions