I am using an API and decoded the JSON array into a PHP array, but it isn't giving me the specific values when I use a foreach loop. Here is a snippet of the original JSON array:
"playerCredentials": {
"observerEncryptionKey": "blahblah",
"dataVersion": 0,
"playerId": 8675309,
"serverPort": 0,
"observer": true,
"summonerId": 0,
"championId": 0,
"observerServerIp": "111.111.111.111",
"gameId": 123456789,
"observerServerPort": 4388,
"lastSelectedSkinIndex": 0
},
then, I ran this code:
$array = json_decode($json_array, true);
which then turned the above into:
{ ["playerCredentials"]=> array(11)
{
["observerEncryptionKey"]=> string(32) "blahblah"
["dataVersion"]=> int(0)
["playerId"]=> int(8675309)
["serverPort"]=> int(0)
["observer"]=> bool(true)
["summonerId"]=> int(0)
["championId"]=> int(0)
["observerServerIp"]=> string(14) "111.111.111.111"
["gameId"]=> int(123456789)
["observerServerPort"]=> int(4338)
["lastSelectedSkinIndex"]=> int(0)
}
however, when I run this foreach loop:
foreach($array['playerCredentials'] as $stats) {
echo $stats['playerId'];
}
all I get as a return is 82 (I don't even know where that comes from). However, if I run this:
foreach($array['playerCredentials'] as $stats) {
echo $stats."<br>";
}
I get all of the information in the whole array:
blahblah
0
8675309
0
true
0
0
111.111.111.111
123456789
4338
0
How can I just get one piece of it?
{
"playerCredentials": {
"observerEncryptionKey": "blahblah",
"dataVersion": 0,
"playerId": 8675309,
"serverPort": 0,
"observer": true,
"summonerId": 0,
"championId": 0,
"observerServerIp": "111.111.111.111",
"gameId": 1347503269,
"observerServerPort": 8088,
"lastSelectedSkinIndex": 0
},
"dataVersion": 0,
"gameName": "match-1347503269",
"reconnectDelay": 0,
"game": {
"practiceGameRewardsDisabledReasons": {
"array": []
},
"glmSecurePort": 0,
"queuePosition": 0,
"playerChampionSelections": {
"array": [
{
"spell1Id": 4,
"spell2Id": 7,
"championId": 25,
"summonerInternalName": "nameone",
"selectedSkinIndex": 0,
"dataVersion": 0
},
{
"spell1Id": 12,
"spell2Id": 4,
"championId": 13,
"summonerInternalName": "nametwo",
"selectedSkinIndex": 0,
"dataVersion": 0
}
]