2016-11-05 60 views
0

我從JSON獲取數據時遇到困難。從PHP中訪問JSON數據

我做一個foreach()。因我從一個API獲取JSON格式的數據後,但我不能得到它的工作。

這裏是我的代碼:

的$ player_runes是數據庫變量,它的工作原理完全好,我的問題越來越runeID的數據爲每個陣列(頁)

foreach($player_runes->pages as $statplayerrunesfor){ 

$statplayerrunesforune0 = $statplayerrunesfor->slots->runeId; 

echo $statplayerrunesforune0; 

} 

這裏是JSON數據:

{ 
"29161162": { 
"summonerId": 29161162, 
"pages": [ 
    { 
    "id": 24193964, 
    "name": "nida", 
    "current": false, 
    "slots": [ 
     { 
     "runeSlotId": 1, 
     "runeId": 5273 
     }, 
     { 
     "runeSlotId": 2, 
     "runeId": 5273 
     }, 
     { 
     "runeSlotId": 3, 
     "runeId": 5273 
     }, 
     { 
     "runeSlotId": 4, 
     "runeId": 5273 
     }, 
     { 
     "runeSlotId": 5, 
     "runeId": 5273 
     }, 
     { 
     "runeSlotId": 6, 
     "runeId": 5273 
     }, 
     { 
     "runeSlotId": 7, 
     "runeId": 5273 
     }, 
     { 
     "runeSlotId": 8, 
     "runeId": 5273 
     }, 
     { 
     "runeSlotId": 9, 
     "runeId": 5273 
     }, 
     { 
     "runeSlotId": 10, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 11, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 12, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 13, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 14, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 15, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 16, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 17, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 18, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 19, 
     "runeId": 5297 
     }, 
     { 
     "runeSlotId": 20, 
     "runeId": 5297 
     }, 
     { 
     "runeSlotId": 21, 
     "runeId": 5297 
     }, 
     { 
     "runeSlotId": 22, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 23, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 24, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 25, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 26, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 27, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 28, 
     "runeId": 5357 
     }, 
     { 
     "runeSlotId": 29, 
     "runeId": 5357 
     }, 
     { 
     "runeSlotId": 30, 
     "runeId": 5357 
     } 
    ] 
    }, 
    { 
    "id": 24193965, 
    "name": "adc", 
    "current": false, 
    "slots": [ 
     { 
     "runeSlotId": 1, 
     "runeId": 5245 
     }, 
     { 
     "runeSlotId": 2, 
     "runeId": 5245 
     }, 
     { 
     "runeSlotId": 3, 
     "runeId": 5245 
     }, 
     { 
     "runeSlotId": 4, 
     "runeId": 5245 
     }, 
     { 
     "runeSlotId": 5, 
     "runeId": 5245 
     }, 
     { 
     "runeSlotId": 6, 
     "runeId": 5245 
     }, 
     { 
     "runeSlotId": 7, 
     "runeId": 5245 
     }, 
     { 
     "runeSlotId": 8, 
     "runeId": 5245 
     }, 
     { 
     "runeSlotId": 9, 
     "runeId": 5245 
     }, 
     { 
     "runeSlotId": 10, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 11, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 12, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 13, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 14, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 15, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 16, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 17, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 18, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 19, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 20, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 21, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 22, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 23, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 24, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 25, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 26, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 27, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 28, 
     "runeId": 5337 
     }, 
     { 
     "runeSlotId": 29, 
     "runeId": 5337 
     }, 
     { 
     "runeSlotId": 30, 
     "runeId": 5337 
     } 
    ] 
    }, 
    { 
    "id": 60148113, 
    "name": "zed", 
    "current": false, 
    "slots": [ 
     { 
     "runeSlotId": 1, 
     "runeId": 5253 
     }, 
     { 
     "runeSlotId": 2, 
     "runeId": 5253 
     }, 
     { 
     "runeSlotId": 3, 
     "runeId": 5253 
     }, 
     { 
     "runeSlotId": 4, 
     "runeId": 5253 
     }, 
     { 
     "runeSlotId": 5, 
     "runeId": 5253 
     }, 
     { 
     "runeSlotId": 6, 
     "runeId": 5253 
     }, 
     { 
     "runeSlotId": 7, 
     "runeId": 5253 
     }, 
     { 
     "runeSlotId": 8, 
     "runeId": 5245 
     }, 
     { 
     "runeSlotId": 9, 
     "runeId": 5245 
     }, 
     { 
     "runeSlotId": 10, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 11, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 12, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 13, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 14, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 15, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 16, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 17, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 18, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 19, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 20, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 21, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 22, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 23, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 24, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 25, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 26, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 27, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 28, 
     "runeId": 5345 
     }, 
     { 
     "runeSlotId": 29, 
     "runeId": 5345 
     }, 
     { 
     "runeSlotId": 30, 
     "runeId": 5335 
     } 
    ] 
    }, 
    { 
    "id": 60148114, 
    "name": "one", 
    "current": true, 
    "slots": [ 
     { 
     "runeSlotId": 1, 
     "runeId": 5253 
     }, 
     { 
     "runeSlotId": 2, 
     "runeId": 5253 
     }, 
     { 
     "runeSlotId": 3, 
     "runeId": 5253 
     }, 
     { 
     "runeSlotId": 4, 
     "runeId": 5253 
     }, 
     { 
     "runeSlotId": 5, 
     "runeId": 5253 
     }, 
     { 
     "runeSlotId": 6, 
     "runeId": 5253 
     }, 
     { 
     "runeSlotId": 7, 
     "runeId": 5253 
     }, 
     { 
     "runeSlotId": 8, 
     "runeId": 5253 
     }, 
     { 
     "runeSlotId": 9, 
     "runeId": 5253 
     }, 
     { 
     "runeSlotId": 10, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 11, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 12, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 13, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 14, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 15, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 16, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 17, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 18, 
     "runeId": 5317 
     }, 
     { 
     "runeSlotId": 19, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 20, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 21, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 22, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 23, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 24, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 25, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 26, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 27, 
     "runeId": 5289 
     }, 
     { 
     "runeSlotId": 28, 
     "runeId": 5335 
     }, 
     { 
     "runeSlotId": 29, 
     "runeId": 5335 
     }, 
     { 
     "runeSlotId": 30, 
     "runeId": 5335 
     } 
    ] 
    } 
] 
} 
} 
+0

json_decode();閱讀它 –

+0

我已經有它解碼,我的問題是我如何獲得數據的符號runeId就像我在foreach中做了什麼? – ForsakenHound

+0

檢查我的答案,如果你使用'json_decode()'它變成一個數組,而不是對象。 –

回答

0

它應該爲你工作:

$player_runes = json_decode($variable_with_json, true); 
foreach($player_runes['pages'] as $statplayerrunesfor){ 

$statplayerrunesforune0 = $statplayerrunesfor['slots']['runeId']; 

echo $statplayerrunesforune0; 

} 

解碼後它變成了一個不是對象的array

+0

不起作用,但我已經將其解碼爲一個數組,問題是我嘗試獲取數據的部分 - > runeId。有些原因不會使頁面變黑。 – ForsakenHound

+0

無論如何,它總是更好地找出你自己的解決方案:) –

0

試試這個:

foreach(json_decode($player_runes->pages) as $statplayerrunesfor){ 

    $statplayerrunesforune0 = $statplayerrunesfor->{'slots'}->{'runeId'}; 

    echo $statplayerrunesforune0; 

} 

希望工程:)

+0

不起作用,但我已經解碼成一個數組問題是我嘗試獲取數據的部分 - > runeId。有些原因不會使頁面變黑。 – ForsakenHound

+0

@ForsakenHound你是否在'json_decode'中使用'true'選項?然後它變成'$ player_runes ['pages']'。 – Barmar