简体   繁体   中英

PHP - Decode JSON array and use in foreach loop

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

In your loops, $stats is referring to the value of each element in the array. I think you're looking for $array['playerCredentials']['playerId']; . If you want to iterate over all properties of a player, you could do this:

foreach ($array['playerCredentials'] as $key => $value) {
    printf('%s => %s<br />', $key, $value);
}

You can try like this

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


$array = json_decode($json_array, true);

foreach($array as $playerCredentials) {
echo $playerCredentials['playerId'];
}

Output

8675309

demo

You could do it like this if using PHP > 5.4:

<?php
$json='{
    "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
    }
}';


$playerCredentials = json_decode($json, true)["playerCredentials"];

foreach($playerCredentials as $key => $value) {
    echo "key: ".$key."\n";
    echo "value: ".$value."\n";
}

Here's a demo: https://eval.in/137205

Or, if using PHP < 5.4, you wouldn't be able to nest the array access with json_decode, so you'd just do it like this:

<?php
$json='{
    "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
    }
}';


$result = json_decode($json, true);

foreach($result["playerCredentials"] as $key => $value) {
    echo "key: ".$key."\n";
    echo "value: ".$value."\n";
}

Here's a demo: https://eval.in/137209

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM