How to use foreach with this type of JSON (PHP)

Asked

Viewed 10,706 times

2

I am trying to display the values of a JSON with PHP more unsuccessfully. Code Used:

JSON

{
    "friendslist": {
        "friends": [
            {
                "steamid": "76561197960265731",
                "relationship": "friend",
                "friend_since": 0
            },
            {
                "steamid": "76561197960265738",
                "relationship": "friend",
                "friend_since": 0
            },
            {
                "steamid": "76561197960265740",
                "relationship": "friend",
                "friend_since": 0
            },
            {
                "steamid": "76561197960265747",
                "relationship": "friend",
                "friend_since": 0
            }
        ]
    }
}
PHP
$steamid_player = "76561198112612121";
        $apikey = "APIKEY";

       $amg = file_get_contents("http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key=$apikey&steamid=$steamid_player&relationship=friend");
        $decode = json_decode($amg, TRUE);

        foreach ($decode["friendslist"]["friends"][0] as $valor){

            $steamid = $valor["relationship"]->steamid;

            echo $steamid;
        }

Error returned


Warning: Illegal string offset 'relationship' in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\projects\leo\test.php on line 10

Notice: Trying to get property of non-object in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\projects\leo\test.php on line 10

Warning: Illegal string offset 'relationship' in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\projects\leo\test.php on line 10

Notice: Trying to get property of non-object in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\projects\leo\test.php on line 10

Notice: Trying to get property of non-object in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\projects\leo\test.php on line 10

But an error is always returned , and so no value is displayed. Where am I going wrong ?

  • Which error is generating, on which line?

  • I went through it now and ended up solving it. What you have to be sure of first is whether the $Decode is returning you a array since the foreach expecting a array.

1 answer

10


It follows a simplification, and syntax correction:

$decode = json_decode( $amg, TRUE );
foreach ( $decode["friendslist"]["friends"] as $valor){
    $steamid = $valor["steamid"];
    echo $steamid;
}

See working on IDEONE.

You were calling the index [0] in the foreach, and he’s already going to the $valor. In addition, he was treating the return as an object, and the json_decode is returning array associative.


Object version

See the syntax difference by swapping the TRUE of json_decode():

$decode = json_decode( $amg, FALSE );
foreach ( $decode->friendslist->friends as $valor){
    $steamid = $valor->steamid;
    echo $steamid;
}

Here is also a demo on IDEONE.

The second parameter of json_decode precisely determines whether the return will be an object or a array associative.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.