好,这个标题可能有点误导人,但我不太确定我在描述什么,所以这里。
我有以下JSON:
{
"lastModified": 1368517749000,
"name": "Requiem Paradisum",
"realm": "Chamber of Aspects",
"battlegroup": "Misery",
"level": 25,
"side": 1,
"achievementPoints": 1710,
"emblem": {
"icon": 126,
"iconColor": "ffdfa55a",
"border": 3,
"borderColor": "ff0f1415",
"backgroundColor": "ff232323"
},
"news": [
{
"type": "itemPurchase",
"character": "Osmoses",
"timestamp": 1368482100000,
"itemId": 91781
},
{
"type": "itemLoot",
"character": "Greenmean",
"timestamp": 1368477900000,
"itemId": 87209
},
{
"type": "itemLoot",
"character": "Greenmean",
"timestamp": 1368475800000,
"itemId": 86880
},
{
"type": "itemPurchase",
"character": "Osmoses",
"timestamp": 1368475380000,
"itemId": 91781
},
{
"type": "itemPurchase",
"character": "Osmoses",
"timestamp": 1368475380000,
"itemId": 91779
},
{
"type": "itemPurchase",
"character": "Osmoses",
"timestamp": 1368475320000,
"itemId": 91779
},
{
"type": "playerAchievement",
"character": "Osmoses",
"timestamp": 1368470700000,
"achievement": {
"id": 6193,
"title": "Level 90",
"points": 10,
"description": "Reach level 90.",
"rewardItems": [
{
"id": 87764,
"name": "Serpent's Heart Firework",
"icon": "inv_misc_missilelarge_green",
"quality": 1,
"itemLevel": 1,
"tooltipParams": {
},
"stats": [
],
"armor": 0
}
],
"icon": "achievement_level_90",
"criteria": [
],
"accountWide": false,
"factionId": 2
}
},
基本上我需要遍历"news"中的所有内容并输出它。我不知道如何正确解析它:答:不指定密钥号码和B:当它到达一个键,然后在这些键下包含更多的键和更多的数组时,我就不知所措了。(例如"玩家成就"键)我很感激我在这里可能是一个新手,很可能在"php傻瓜"的第一页,但我发誓我已经谷歌死了!
看看这种方法是否合适。不过,请确保JSON数组的格式正确。
$test = the_json_array;
$array = json_decode($test,true);
function recursive($array) {
foreach($array as $key => $value) {
if (!is_array($value)) echo $key.":".$value."<br/>";
else recursive($value);
}
}
recursive($array);
参见json_decode
不要忘记给第二个参数作为TRUE
,否则它将返回对象
,并尝试像这样
$json = 'your json'
$json_array = json_decode($json,true);
$news = $json_array['news'];
foreach($news as $value)
{
print_r($value);
}
我想你应该看看array_walk_recursive
function printResult($item, $key)
{
echo "$key holds $itemn";
}
array_walk_recursive($news, 'printResult');
你需要做json_decode('your-jason', True)
,它会将你的Json字符串转换为数组。
Json_decode了解更多
如果在jason_decode
函数中没有指定TRUE
,那么它将返回php对象
希望回答这个问题。关于