2015-10-19 56 views
0

我试图从json数组中回显[steamid]。我不断收到一条错误消息:“注意:未定义的索引:朋友”。我试图创建一个循环,只回应蒸汽,但我认为我没有针对[蒸汽]子元素。Php echo json array with children

<?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']; 
} 

?> 

这他的JSON文件的外观:

{ 
"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 
     } 
    ] 

} 

}

+2

隐藏你的钥匙:) –

回答

0

你不是通过正确的数组循环(如上面samlev说)......

<?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']; 
    } 

?> 
+1

传递TRUE;第二个参数使得'json_decode'返回一个关联数组,而不是一个对象。 – samlev

+0

你是对的,完全错过了!我会编辑我的答案 – Lynch

3

当你foreach的朋友列表,你会发现friends阵列,它没有ve一键friends

取而代之的是直接获取friends属性,并且foreach。

<?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']; 
} 

?> 
+0

谢谢!这工作 – elw