[英]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. 我正在使用API,并将JSON数组解码为PHP数组,但是当我使用foreach循环时,它没有提供特定的值。 Here is a snippet of the original JSON array:
这是原始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
},
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循环时:
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). 我得到的回报是82(我什至不知道那是哪里来的)。 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. 在循环中,
$stats
引用数组中每个元素的值。 I think you're looking for $array['playerCredentials']['playerId'];
我认为您正在寻找
$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
8675309
You could do it like this if using PHP > 5.4: 如果使用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 这是一个演示: 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 <5.4,您将无法使用json_decode嵌套数组访问权限,因此您可以这样做:
<?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 这是一个演示: https : //eval.in/137209
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.