在PHP中从JSON访问数据 [英] Accessing data from JSON in PHP
本文介绍了在PHP中从JSON访问数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很难从JSON获取数据.
I am having difficulty getting data from JSON.
我对从API获得的json格式数据进行了foreach()处理后,但无法正常工作.
I am after doing a foreach() for json formatted data that I get from a API, but I cant get it to work.
这是我的代码:
$ player_runes是数据库变量,它完全可以正常工作,我的问题是获取每个数组(页面)的runeID数据
The $player_runes is the database variable, it works totally fine, my problem is getting the data of runeID for each Array (pages)
foreach($player_runes->pages as $statplayerrunesfor){
$statplayerrunesforune0 = $statplayerrunesfor->slots->runeId;
echo $statplayerrunesforune0;
}
这是JSON数据:
{
"29161162": {
"summonerId": 29161162,
"pages": [
{
"id": 24193964,
"name": "nida",
"current": false,
"slots": [
{
"runeSlotId": 1,
"runeId": 5273
},
{
"runeSlotId": 2,
"runeId": 5273
},
{
"runeSlotId": 3,
"runeId": 5273
},
{
"runeSlotId": 4,
"runeId": 5273
},
{
"runeSlotId": 5,
"runeId": 5273
},
{
"runeSlotId": 6,
"runeId": 5273
},
{
"runeSlotId": 7,
"runeId": 5273
},
{
"runeSlotId": 8,
"runeId": 5273
},
{
"runeSlotId": 9,
"runeId": 5273
},
{
"runeSlotId": 10,
"runeId": 5317
},
{
"runeSlotId": 11,
"runeId": 5317
},
{
"runeSlotId": 12,
"runeId": 5317
},
{
"runeSlotId": 13,
"runeId": 5317
},
{
"runeSlotId": 14,
"runeId": 5317
},
{
"runeSlotId": 15,
"runeId": 5317
},
{
"runeSlotId": 16,
"runeId": 5317
},
{
"runeSlotId": 17,
"runeId": 5317
},
{
"runeSlotId": 18,
"runeId": 5317
},
{
"runeSlotId": 19,
"runeId": 5297
},
{
"runeSlotId": 20,
"runeId": 5297
},
{
"runeSlotId": 21,
"runeId": 5297
},
{
"runeSlotId": 22,
"runeId": 5289
},
{
"runeSlotId": 23,
"runeId": 5289
},
{
"runeSlotId": 24,
"runeId": 5289
},
{
"runeSlotId": 25,
"runeId": 5289
},
{
"runeSlotId": 26,
"runeId": 5289
},
{
"runeSlotId": 27,
"runeId": 5289
},
{
"runeSlotId": 28,
"runeId": 5357
},
{
"runeSlotId": 29,
"runeId": 5357
},
{
"runeSlotId": 30,
"runeId": 5357
}
]
},
{
"id": 24193965,
"name": "adc",
"current": false,
"slots": [
{
"runeSlotId": 1,
"runeId": 5245
},
{
"runeSlotId": 2,
"runeId": 5245
},
{
"runeSlotId": 3,
"runeId": 5245
},
{
"runeSlotId": 4,
"runeId": 5245
},
{
"runeSlotId": 5,
"runeId": 5245
},
{
"runeSlotId": 6,
"runeId": 5245
},
{
"runeSlotId": 7,
"runeId": 5245
},
{
"runeSlotId": 8,
"runeId": 5245
},
{
"runeSlotId": 9,
"runeId": 5245
},
{
"runeSlotId": 10,
"runeId": 5317
},
{
"runeSlotId": 11,
"runeId": 5317
},
{
"runeSlotId": 12,
"runeId": 5317
},
{
"runeSlotId": 13,
"runeId": 5317
},
{
"runeSlotId": 14,
"runeId": 5317
},
{
"runeSlotId": 15,
"runeId": 5317
},
{
"runeSlotId": 16,
"runeId": 5317
},
{
"runeSlotId": 17,
"runeId": 5317
},
{
"runeSlotId": 18,
"runeId": 5317
},
{
"runeSlotId": 19,
"runeId": 5289
},
{
"runeSlotId": 20,
"runeId": 5289
},
{
"runeSlotId": 21,
"runeId": 5289
},
{
"runeSlotId": 22,
"runeId": 5289
},
{
"runeSlotId": 23,
"runeId": 5289
},
{
"runeSlotId": 24,
"runeId": 5289
},
{
"runeSlotId": 25,
"runeId": 5289
},
{
"runeSlotId": 26,
"runeId": 5289
},
{
"runeSlotId": 27,
"runeId": 5289
},
{
"runeSlotId": 28,
"runeId": 5337
},
{
"runeSlotId": 29,
"runeId": 5337
},
{
"runeSlotId": 30,
"runeId": 5337
}
]
},
{
"id": 60148113,
"name": "zed",
"current": false,
"slots": [
{
"runeSlotId": 1,
"runeId": 5253
},
{
"runeSlotId": 2,
"runeId": 5253
},
{
"runeSlotId": 3,
"runeId": 5253
},
{
"runeSlotId": 4,
"runeId": 5253
},
{
"runeSlotId": 5,
"runeId": 5253
},
{
"runeSlotId": 6,
"runeId": 5253
},
{
"runeSlotId": 7,
"runeId": 5253
},
{
"runeSlotId": 8,
"runeId": 5245
},
{
"runeSlotId": 9,
"runeId": 5245
},
{
"runeSlotId": 10,
"runeId": 5317
},
{
"runeSlotId": 11,
"runeId": 5317
},
{
"runeSlotId": 12,
"runeId": 5317
},
{
"runeSlotId": 13,
"runeId": 5317
},
{
"runeSlotId": 14,
"runeId": 5317
},
{
"runeSlotId": 15,
"runeId": 5317
},
{
"runeSlotId": 16,
"runeId": 5317
},
{
"runeSlotId": 17,
"runeId": 5317
},
{
"runeSlotId": 18,
"runeId": 5317
},
{
"runeSlotId": 19,
"runeId": 5289
},
{
"runeSlotId": 20,
"runeId": 5289
},
{
"runeSlotId": 21,
"runeId": 5289
},
{
"runeSlotId": 22,
"runeId": 5289
},
{
"runeSlotId": 23,
"runeId": 5289
},
{
"runeSlotId": 24,
"runeId": 5289
},
{
"runeSlotId": 25,
"runeId": 5289
},
{
"runeSlotId": 26,
"runeId": 5289
},
{
"runeSlotId": 27,
"runeId": 5289
},
{
"runeSlotId": 28,
"runeId": 5345
},
{
"runeSlotId": 29,
"runeId": 5345
},
{
"runeSlotId": 30,
"runeId": 5335
}
]
},
{
"id": 60148114,
"name": "one",
"current": true,
"slots": [
{
"runeSlotId": 1,
"runeId": 5253
},
{
"runeSlotId": 2,
"runeId": 5253
},
{
"runeSlotId": 3,
"runeId": 5253
},
{
"runeSlotId": 4,
"runeId": 5253
},
{
"runeSlotId": 5,
"runeId": 5253
},
{
"runeSlotId": 6,
"runeId": 5253
},
{
"runeSlotId": 7,
"runeId": 5253
},
{
"runeSlotId": 8,
"runeId": 5253
},
{
"runeSlotId": 9,
"runeId": 5253
},
{
"runeSlotId": 10,
"runeId": 5317
},
{
"runeSlotId": 11,
"runeId": 5317
},
{
"runeSlotId": 12,
"runeId": 5317
},
{
"runeSlotId": 13,
"runeId": 5317
},
{
"runeSlotId": 14,
"runeId": 5317
},
{
"runeSlotId": 15,
"runeId": 5317
},
{
"runeSlotId": 16,
"runeId": 5317
},
{
"runeSlotId": 17,
"runeId": 5317
},
{
"runeSlotId": 18,
"runeId": 5317
},
{
"runeSlotId": 19,
"runeId": 5289
},
{
"runeSlotId": 20,
"runeId": 5289
},
{
"runeSlotId": 21,
"runeId": 5289
},
{
"runeSlotId": 22,
"runeId": 5289
},
{
"runeSlotId": 23,
"runeId": 5289
},
{
"runeSlotId": 24,
"runeId": 5289
},
{
"runeSlotId": 25,
"runeId": 5289
},
{
"runeSlotId": 26,
"runeId": 5289
},
{
"runeSlotId": 27,
"runeId": 5289
},
{
"runeSlotId": 28,
"runeId": 5335
},
{
"runeSlotId": 29,
"runeId": 5335
},
{
"runeSlotId": 30,
"runeId": 5335
}
]
}
]
}
}
推荐答案
尝试一下:
foreach(json_decode($player_runes->pages) as $statplayerrunesfor){
$statplayerrunesforune0 = $statplayerrunesfor->{'slots'}->{'runeId'};
echo $statplayerrunesforune0;
}
希望它能起作用:)
这篇关于在PHP中从JSON访问数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文