elw
elw

Reputation: 157

Php echo json array with children

I'm trying to echo the [steamid] from a json array. I keep getting an error message: "Notice: Undefined index: friends". I tried to create a loop and echo only the steamid's but I think i'm failing to target the [steamid] child element.

<?php

$jsondata = file_get_contents('http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key=<api key>&steamid=<steamid>&relationship=friend');

$data = json_decode($jsondata, true);

foreach ($data['friendslist'] AS $d){
echo $d['friends'];
}

?>

This his how the json file looks:

    {
"friendslist": {
    "friends": [
        {
            "steamid": "76561197961044519",
            "relationship": "friend",
            "friend_since": 1267024607
        },
        {
            "steamid": "76561197962344290",
            "relationship": "friend",
            "friend_since": 1228163456
        },
        {
            "steamid": "76561197963830805",
            "relationship": "friend",
            "friend_since": 1369592065
        },
        {
            "steamid": "76561197965836538",
            "relationship": "friend",
            "friend_since": 0
        },
        {
            "steamid": "76561197965993321",
            "relationship": "friend",
            "friend_since": 0
        },
        {
            "steamid": "76561197968262516",
            "relationship": "friend",
            "friend_since": 0
        },
        {
            "steamid": "76561197968319327",
            "relationship": "friend",
            "friend_since": 0
        },
        {
            "steamid": "76561197968678768",
            "relationship": "friend",
            "friend_since": 1302485493
        },
        {
            "steamid": "76561197970080628",
            "relationship": "friend",
            "friend_since": 0
        },
        {
            "steamid": "76561197970103578",
            "relationship": "friend",
            "friend_since": 0
        },
        {
            "steamid": "76561197970211993",
            "relationship": "friend",
            "friend_since": 0
        },
        {
            "steamid": "76561197970236138",
            "relationship": "friend",
            "friend_since": 0
        },
        {
            "steamid": "76561197970423745",
            "relationship": "friend",
            "friend_since": 0
        },
        {
            "steamid": "76561197971123358",
            "relationship": "friend",
            "friend_since": 0
        },
        {
            "steamid": "76561197971929441",
            "relationship": "friend",
            "friend_since": 1363120259
        },
        {
            "steamid": "76561197972021368",
            "relationship": "friend",
            "friend_since": 0
        },
        {
            "steamid": "76561197972063782",
            "relationship": "friend",
            "friend_since": 0
        },
        {
            "steamid": "76561197972284446",
            "relationship": "friend",
            "friend_since": 0
        },
        {
            "steamid": "76561197972439523",
            "relationship": "friend",
            "friend_since": 0
        },
        {
            "steamid": "76561197972561841",
            "relationship": "friend",
            "friend_since": 1249845285
        },
        {
            "steamid": "76561197973368234",
            "relationship": "friend",
            "friend_since": 0
        },
        {
            "steamid": "76561197973592555",
            "relationship": "friend",
            "friend_since": 0
        },
        {
            "steamid": "76561197973763054",
            "relationship": "friend",
            "friend_since": 1258696202
        },
        {
            "steamid": "76561197975755140",
            "relationship": "friend",
            "friend_since": 1250467103
        },
        {
            "steamid": "76561197976846029",
            "relationship": "friend",
            "friend_since": 1229369143
        },
        {
            "steamid": "76561197977335852",
            "relationship": "friend",
            "friend_since": 0
        },
        {
            "steamid": "76561197977513014",
            "relationship": "friend",
            "friend_since": 1367005679
        },
        {
            "steamid": "76561197978885198",
            "relationship": "friend",
            "friend_since": 1249844860
        },
        {
            "steamid": "76561197979916873",
            "relationship": "friend",
            "friend_since": 1249762821
        },
        {
            "steamid": "76561197979955780",
            "relationship": "friend",
            "friend_since": 1264441242
        },
        {
            "steamid": "76561197980121694",
            "relationship": "friend",
            "friend_since": 1250949580
        },
        {
            "steamid": "76561197980575831",
            "relationship": "friend",
            "friend_since": 0
        },
        {
            "steamid": "76561197983622552",
            "relationship": "friend",
            "friend_since": 1250454117
        },
        {
            "steamid": "76561197985048429",
            "relationship": "friend",
            "friend_since": 1258934104
        },
        {
            "steamid": "76561197986706015",
            "relationship": "friend",
            "friend_since": 0
        },
        {
            "steamid": "76561197987020927",
            "relationship": "friend",
            "friend_since": 1328982385
        },
        {
            "steamid": "76561197990742853",
            "relationship": "friend",
            "friend_since": 1247785687
        },
        {
            "steamid": "76561197995299179",
            "relationship": "friend",
            "friend_since": 1239546286
        },
        {
            "steamid": "76561197999249574",
            "relationship": "friend",
            "friend_since": 1248405186
        },
        {
            "steamid": "76561198002376620",
            "relationship": "friend",
            "friend_since": 0
        },
        {
            "steamid": "76561198005497463",
            "relationship": "friend",
            "friend_since": 1235984543
        },
        {
            "steamid": "76561198006905726",
            "relationship": "friend",
            "friend_since": 1424246620
        },
        {
            "steamid": "76561198009614201",
            "relationship": "friend",
            "friend_since": 1258408696
        },
        {
            "steamid": "76561198014997781",
            "relationship": "friend",
            "friend_since": 1328359560
        },
        {
            "steamid": "76561198016288250",
            "relationship": "friend",
            "friend_since": 1275917452
        },
        {
            "steamid": "76561198024847025",
            "relationship": "friend",
            "friend_since": 1334414245
        },
        {
            "steamid": "76561198024929479",
            "relationship": "friend",
            "friend_since": 1330779187
        },
        {
            "steamid": "76561198025398804",
            "relationship": "friend",
            "friend_since": 1363120230
        },
        {
            "steamid": "76561198025910770",
            "relationship": "friend",
            "friend_since": 1400411500
        },
        {
            "steamid": "76561198030592022",
            "relationship": "friend",
            "friend_since": 1347301437
        },
        {
            "steamid": "76561198032035158",
            "relationship": "friend",
            "friend_since": 1423965886
        }
    ]

}

}

Upvotes: 0

Views: 4552

Answers (2)

Lynch
Lynch

Reputation: 910

You're not looping through the correct array (as samlev says above)...

<?php

    $jsondata = file_get_contents('http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key=<api key>&steamid=<steam id>&relationship=friend');

    $data = json_decode($jsondata, true);

    foreach ($data['friendslist']['friends'] AS $d){
        echo $d['steamid'];
    }

?>

Upvotes: 0

samlev
samlev

Reputation: 5942

When you foreach friendlist, you're finding the friends array, which doesn't have a key friends.

Instead just get the friends property directly, and foreach that.

<?php

$jsondata = file_get_contents('http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key=<api key>&steamid=<steam id>&relationship=friend');

$data = json_decode($jsondata, true);

foreach ($data['friendslist']['friends'] AS $d){
    echo $d['steamid'];
}

?>

Upvotes: 4

Related Questions