将PHP输出文本转换为可读的内容

时间:2014-01-13 14:37:36

标签: php

我对PHP知之甚少,所以我认为你们可以提供帮助,这就是我的问题。我正在使用API​​,特别是Riot的API来从那里获取一些东西,无论如何我想知道是否可以转换它:

{
 "id": 18420146,
 "name": "AP [Ahri]",
 "current": false,
 "slots": [{
     "runeSlotId": 2,
     "rune": {
         "id": 5273,
         "name": "Greater Mark of Magic Penetration",
         "description": "+0.87 magic penetration",
         "tier": 3
     }
 }, {
     "runeSlotId": 3,
     "rune": {
         "id": 5273,
         "name": "Greater Mark of Magic Penetration",
         "description": "+0.87 magic penetration",
         "tier": 3
     }
 }, {
     "runeSlotId": 1,
     "rune": {
         "id": 5273,
         "name": "Greater Mark of Magic Penetration",
         "description": "+0.87 magic penetration",
         "tier": 3
     }
 }, {
     "runeSlotId": 6,
     "rune": {
         "id": 5273,
         "name": "Greater Mark of Magic Penetration",
         "description": "+0.87 magic penetration",
         "tier": 3
     }
 }, {
     "runeSlotId": 7,
     "rune": {
         "id": 5273,
         "name": "Greater Mark of Magic Penetration",
         "description": "+0.87 magic penetration",
         "tier": 3
     }
 }, {
     "runeSlotId": 4,
     "rune": {
         "id": 5273,
         "name": "Greater Mark of Magic Penetration",
         "description": "+0.87 magic penetration",
         "tier": 3
     }
 }, {
     "runeSlotId": 5,
     "rune": {
         "id": 5273,
         "name": "Greater Mark of Magic Penetration",
         "description": "+0.87 magic penetration",
         "tier": 3
     }
 }, {
     "runeSlotId": 9,
     "rune": {
         "id": 5273,
         "name": "Greater Mark of Magic Penetration",
         "description": "+0.87 magic penetration",
         "tier": 3
     }
 }, {
     "runeSlotId": 8,
     "rune": {
         "id": 5273,
         "name": "Greater Mark of Magic Penetration",
         "description": "+0.87 magic penetration",
         "tier": 3
     }
 }, {
     "runeSlotId": 30,
     "rune": {
         "id": 5357,
         "name": "Greater Quintessence of Ability Power",
         "description": "+4.95 ability power",
         "tier": 3
     }
 }, {
     "runeSlotId": 22,
     "rune": {
         "id": 5289,
         "name": "Greater Glyph of Magic Resist",
         "description": "+1.34 magic resist",
         "tier": 3
     }
 }, {
     "runeSlotId": 23,
     "rune": {
         "id": 5289,
         "name": "Greater Glyph of Magic Resist",
         "description": "+1.34 magic resist",
         "tier": 3
     }
 }, {
     "runeSlotId": 28,
     "rune": {
         "id": 5357,
         "name": "Greater Quintessence of Ability Power",
         "description": "+4.95 ability power",
         "tier": 3
     }
 }, {
     "runeSlotId": 20,
     "rune": {
         "id": 5289,
         "name": "Greater Glyph of Magic Resist",
         "description": "+1.34 magic resist",
         "tier": 3
     }
 }, {
     "runeSlotId": 21,
     "rune": {
         "id": 5289,
         "name": "Greater Glyph of Magic Resist",
         "description": "+1.34 magic resist",
         "tier": 3
     }
 }, {
     "runeSlotId": 29,
     "rune": {
         "id": 5357,
         "name": "Greater Quintessence of Ability Power",
         "description": "+4.95 ability power",
         "tier": 3
     }
 }, {
     "runeSlotId": 19,
     "rune": {
         "id": 5289,
         "name": "Greater Glyph of Magic Resist",
         "description": "+1.34 magic resist",
         "tier": 3
     }
 }, {
     "runeSlotId": 14,
     "rune": {
         "id": 5331,
         "name": "Greater Seal of Mana Regeneration",
         "description": "+0.41 mana regen / 5 sec.",
         "tier": 3
     }
 }, {
     "runeSlotId": 18,
     "rune": {
         "id": 5317,
         "name": "Greater Seal of Armor",
         "description": "+1.41 armor",
         "tier": 3
     }
 }, {
     "runeSlotId": 17,
     "rune": {
         "id": 5317,
         "name": "Greater Seal of Armor",
         "description": "+1.41 armor",
         "tier": 3
     }
 }, {
     "runeSlotId": 16,
     "rune": {
         "id": 5317,
         "name": "Greater Seal of Armor",
         "description": "+1.41 armor",
         "tier": 3
     }
 }, {
     "runeSlotId": 11,
     "rune": {
         "id": 5331,
         "name": "Greater Seal of Mana Regeneration",
         "description": "+0.41 mana regen / 5 sec.",
         "tier": 3
     }
 }, {
     "runeSlotId": 15,
     "rune": {
         "id": 5317,
         "name": "Greater Seal of Armor",
         "description": "+1.41 armor",
         "tier": 3
     }
 }, {
     "runeSlotId": 27,
     "rune": {
         "id": 5289,
         "name": "Greater Glyph of Magic Resist",
         "description": "+1.34 magic resist",
         "tier": 3
     }
 }, {
     "runeSlotId": 10,
     "rune": {
         "id": 5331,
         "name": "Greater Seal of Mana Regeneration",
         "description": "+0.41 mana regen / 5 sec.",
         "tier": 3
     }
 }, {
     "runeSlotId": 26,
     "rune": {
         "id": 5289,
         "name": "Greater Glyph of Magic Resist",
         "description": "+1.34 magic resist",
         "tier": 3
     }
 }, {
     "runeSlotId": 25,
     "rune": {
         "id": 5289,
         "name": "Greater Glyph of Magic Resist",
         "description": "+1.34 magic resist",
         "tier": 3
     }
 }, {
     "runeSlotId": 13,
     "rune": {
         "id": 5331,
         "name": "Greater Seal of Mana Regeneration",
         "description": "+0.41 mana regen / 5 sec.",
         "tier": 3
     }
 }, {
     "runeSlotId": 24,
     "rune": {
         "id": 5289,
         "name": "Greater Glyph of Magic Resist",
         "description": "+1.34 magic resist",
         "tier": 3
     }
 }, {
     "runeSlotId": 12,
     "rune": {
         "id": 5331,
         "name": "Greater Seal of Mana Regeneration",
         "description": "+0.41 mana regen / 5 sec.",
         "tier": 3
     }
 }]

},

就像他们出现在这个网站上一样:

HERE

此图像给出每个符文的id HERE

2 个答案:

答案 0 :(得分:0)

//编辑

抱歉在解析整个问题之前停止阅读。 尝试这样的事情:

$api_response_string = ##your_string_here##;
$api_response = json_decode($api_response_string);


if(!empty($api_response)){
    echo 'Runepage Name: '.$api_response['name'].'<br />';
    echo '<table>';
    foreach($api_response['runes'] as $key => $elem){
         echo '<tr>';
         echo '<th>Runeslot Id '.$elem['runeSlotId'].'</th>';
         echo '<td>'.$elem['name'].', description: '.$elem['description'].', tier: '.$elem['tier'].'</td>';
         echo '</tr>';
    }
    echo '</table>';
}

使用此代码,您可以将json_string转换为数组,然后遍历符文键下的所有元素并将其回显。

通过一点CSS魔术,您可以增强视图以满足您的需求。

史蒂夫

////

我用

function _debug($var){
    echo '<pre>'.print_r($var).'</pre>';
}

史蒂夫

答案 1 :(得分:0)

尝试查看PHP扩展程序PHP JSON。 http://php.net/json

相关功能:http://www.php.net/manual/en/ref.json.php