我无法从JSON获取数据。
我正在为我从API获得的json格式数据做一个foreach(),但是我无法让它工作。
这是我的代码:
$ player_runes是数据库变量,它完全正常,我的问题是获取每个数组(页面)的runeID数据
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
}
]
}
]
}
}
答案 0 :(得分:0)
它应该适合你:
$player_runes = json_decode($variable_with_json, true);
foreach($player_runes['pages'] as $statplayerrunesfor){
$statplayerrunesforune0 = $statplayerrunesfor['slots']['runeId'];
echo $statplayerrunesforune0;
}
解码后,它变为array
而不是对象。
答案 1 :(得分:0)
试试这个:
foreach(json_decode($player_runes->pages) as $statplayerrunesfor){
$statplayerrunesforune0 = $statplayerrunesfor->{'slots'}->{'runeId'};
echo $statplayerrunesforune0;
}
希望它有效:)