-1

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
        }
]
user1895377
  • 201
  • 1
  • 3
  • 17
  • 2
    Because that's not an array. If you had [**error reporting enabled**](http://stackoverflow.com/a/6575502/1438393), you'd have received an error message saying "*Warning: Illegal string offset*". – Amal Murali Apr 15 '14 at 21:07
  • @AmalMurali is there a way that I can get those individual values with the way it is? – user1895377 Apr 15 '14 at 21:10
  • @AmalMurali What's not an array? – George Brighton Apr 15 '14 at 21:12
  • @GeorgeBrighton: In the first case, `$stats` is not an array. My comment may have been a bit vague, but I can't edit it any more :/ Please check [this demo](https://eval.in/137187). – Amal Murali Apr 15 '14 at 21:15
  • @AmalMurali Ah I see. I thought you were referring to something in the decoded array. My bad! – George Brighton Apr 15 '14 at 21:17
  • @user1895377: Your second `foreach` is the correct way to do it (if you want to print all the elements). If you want to access a specific element, take a look at George's answer below. It has an example. – Amal Murali Apr 15 '14 at 21:19

3 Answers3

5

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);
}
George Brighton
  • 5,131
  • 9
  • 27
  • 36
  • So what what the foreach loop look like with your code? – user1895377 Apr 15 '14 at 21:47
  • Okay that worked. Now is there a way to get just one value out of that? Lets say I just want playerId? how can I turn the value of playerId into a PHP variable? – user1895377 Apr 15 '14 at 21:56
  • @user1895377 `$variable = $array['playerCredentials']['playerId'];` – George Brighton Apr 15 '14 at 21:56
  • Okay that worked just the way I want it to. If you look at the original post, I added at the end some more of the array. I want to try to get the championId values from this but I tried `foreach ($array['game'] as $key => $value) { $variable = $array['game']['playerChampionSelections']['array']['championId']; } echo $variable;}` and it gave me a blank screen – user1895377 Apr 15 '14 at 22:09
  • @user1895377 `foreach($array['game']['playerChampionSelections'] as $selection) { echo $selection['championId']; }` should do the trick. – George Brighton Apr 15 '14 at 22:14
0

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

meda
  • 45,103
  • 14
  • 92
  • 122
  • That resulted in nothing on the page unfortunately – user1895377 Apr 15 '14 at 21:11
  • I tried that and I got `Warning: Invalid argument supplied for foreach() in /home/content/70/11524070/html/testing.php on line 13` – user1895377 Apr 15 '14 at 21:44
  • 1
    @user1895377 well it works as you can see in the demo, you should copy paste my code, I think you have a typo on the array – meda Apr 15 '14 at 21:54
  • Okay I got that to work. However, there is a letter "m" after all of the values for some reason. For example, instead of showing 8675309 it shows 8675309m and theres no stray m anywhere in the file. – user1895377 Apr 15 '14 at 21:59
0

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

dcarrith
  • 7,510
  • 2
  • 18
  • 9