Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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
        }
]
share|improve this question
2  
Because that's not an array. If you had error reporting enabled, 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. –  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

3 Answers 3

up vote 3 down vote accepted

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);
}
share|improve this answer
    
So what what the foreach loop look like with your code? –  user1895377 Apr 15 '14 at 21:47
    
@user1895377 See my edit. –  George Brighton Apr 15 '14 at 21:49
    
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

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

share|improve this answer
    
I got Parse error: syntax error, unexpected '[' in /home/content/70/11524070/html/testing.php on line 13 –  user1895377 Apr 15 '14 at 21:50
    
Function array dereferencing was only added in PHP 5.4. –  George Brighton Apr 15 '14 at 21:50
    
What version of PHP are you running? If less than 5.4, you probably have to use the alternate method I added above. –  dcarrith Apr 15 '14 at 21:51

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

share|improve this answer
    
That resulted in nothing on the page unfortunately –  user1895377 Apr 15 '14 at 21:11
    
@user1895377 please try my edit , sorry –  meda Apr 15 '14 at 21:14
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.