2015-10-19 79 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