slixxed
slixxed

Reputation: 15

Decoding multi-value JSON

I'm trying to create a game API thing that requires this decoded, but I'm not sure how (this is just for a certain user, so the values wont be the same)

[
    {
        "Id": 382779,
        "Name": "DarkAge Ninjas"
    },
    {
        "Id": 377291,
        "Name": "Emerald Knights of the Seventh Sanctum"
    },
    {
        "Id": 271454,
        "Name": "Knights of RedCliff"
    },
    {
        "Id": 288278,
        "Name": "Knights of the Splintered Skies "
    },
    {
        "Id": 375307,
        "Name": "Korblox's Empire"
    },
    {
        "Id": 387867,
        "Name": "Ne'Kotikoz"
    },
    {
        "Id": 696519,
        "Name": "Orinthians"
    },
    {
        "Id": 27770,
        "Name": "Retexture Artists Official Channel"
    },
    {
        "Id": 585932,
        "Name": "Retexturing Apprentices "
    },
    {
        "Id": 7,
        "Name": "Roblox"
    },
    {
        "Id": 679727,
        "Name": "ROBLOX Community Staff and Forum Users"
    },
    {
        "Id": 127081,
        "Name": "Roblox Wiki"
    }
]

How can I decode this in PHP so It has a list like

DarkAge Ninjas Emerald Knights of the Seventh Sanctum Knights of RedCliff

etc, and have the Id decoded separately so I can make a clickable link out of it :/

Upvotes: 0

Views: 79

Answers (1)

Saqueib
Saqueib

Reputation: 3520

You will need json_decode to turn json into php array

$api_json = '[
    { "Id": 382779, "Name": "DarkAge Ninjas" }, 
    { "Id": 377291, "Name": "Emerald Knights of the Seventh anctum" }
    ...
]';

$api_data = json_decode($api_json, true);

//Now you can loop over the array and print the `Name`
foreach($api_data as $d) {
   echo $d['Name'];
}

above code will output

DarkAge Ninjas 
Emerald Knights of the Seventh Sanctum 
Knights of RedCliff
...

To make link with ids just add this in above loop

echo '<a href="'. $d['Id'].'">'. $d['Name'].'</a>';

as Ed Cottrell suggested, Read the manual: json_decode to know more

Upvotes: 1

Related Questions