0

I'm having some trouble accessing elements in JSON. Here's my code:

$raw_json = '{
        "players": [
            {
                "steamid": "76561197960435530",
                "communityvisibilitystate": 3,
                "profilestate": 1,
                "personaname": "Robin",
                "lastlogoff": 1365134746,
                "profileurl": "http://steamcommunity.com/id/robinwalker/",
                "avatar": "http://media.steampowered.com/steamcommunity/public/images/avatars/f1/f1dd60a188883caf82d0cbfccfe6aba0af1732d4.jpg",
                "avatarmedium": "http://media.steampowered.com/steamcommunity/public/images/avatars/f1/f1dd60a188883caf82d0cbfccfe6aba0af1732d4_medium.jpg",
                "avatarfull": "http://media.steampowered.com/steamcommunity/public/images/avatars/f1/f1dd60a188883caf82d0cbfccfe6aba0af1732d4_full.jpg",
                "personastate": 0,
                "realname": "Robin Walker",
                "primaryclanid": "103582791429521412",
                "timecreated": 1063407589,
                "loccountrycode": "US",
                "locstatecode": "WA",
                "loccityid": 3961
            }
    }
}';
$data = json_decode($raw_json);
print $data->players{'realname'};

My question is - how can I access arrays like personname or realname?

3 Answers 3

3

you're using curly braces instead of brackets. the right syntax would be

$data->players['realname']

That said, you have an object inside the array so accessing it by key wouldn't work either. You'd need something like this

$data->players[0]

Further, your json was invalid so you should fix it. a helpful tool to validate json is this http://json.parser.online.fr/

Sign up to request clarification or add additional context in comments.

Comments

1

First up, your JSON is not in a valid format, and that decode is not even working. Try

<?php
$raw_json = '
{
    "players": [
        {
            "steamid": "76561197960435530",
            "communityvisibilitystate": 3,
            "profilestate": 1,
            "personaname": "Robin",
            "lastlogoff": 1365134746,
            "profileurl": "http://steamcommunity.com/id/robinwalker/",
            "avatar": "http://media.steampowered.com/steamcommunity/public/images/avatars/f1/f1dd60a188883caf82d0cbfccfe6aba0af1732d4.jpg",
            "avatarmedium": "http://media.steampowered.com/steamcommunity/public/images/avatars/f1/f1dd60a188883caf82d0cbfccfe6aba0af1732d4_medium.jpg",
            "avatarfull": "http://media.steampowered.com/steamcommunity/public/images/avatars/f1/f1dd60a188883caf82d0cbfccfe6aba0af1732d4_full.jpg",
            "personastate": 0,
            "realname": "Robin Walker",
            "primaryclanid": "103582791429521412",
            "timecreated": 1063407589,
            "loccountrycode": "US",
            "locstatecode": "WA",
            "loccityid": 3961
        }
    ]
}
';
$data = json_decode($raw_json);
print_r( $data->players[0]); // or
echo $data->players[0]->personaname;
?>

Comments

0

Try this:

$data = json_decode($raw_json);
echo $data->players['realname'];

Or you can see the data format by

print_r($data);

or

var_dump($data);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.