检查json对象是否包含带有php的特定字符串

时间:2017-01-15 21:12:00

标签: php json api

我正在使用来自battle.net的API,该API使用名为' type'的对象。我需要检查类型是什么,所以我可以显示一个适当的单词来匹配类型。即。如果类型是' itemLoot'显示'已抢劫'或者如果类型是' playerAchievment'展示已经实现了#39;

PHP脚本如下:

foreach($feed->news as $newsfeed) {
                if ($feedcount >= 12) {
                    break;
                }
                    echo $newsfeed->character . PHP_EOL;
                        $type = $newsfeed->type . PHP_EOL;
                        $iteminfo = $newsfeed->itemId . PHP_EOL;
                        $itemnumber = $newsfeed->itemId;
                        if ($type="itemPurchase"){
                            echo " has looted";
                             $itemurl = file_get_contents("https://eu.api.battle.net/wow/item/$itemnumber?locale=en_GB&apikey=f43uf742srjkmvpnk76u52pw5nz5kga5");
                            $itemname = json_decode($itemurl);
                            echo " " . $itemname->name . "<br>";
                        }
                        elseif ($type="itemPurchase"){
                            echo " has purchased";
                        }
                elseif ($type="playerAchievement"){
                    echo " has Achieved<br>";
                }

                    ++$feedcount;

            }

我正在使用的JSON文件是:

{
"lastModified": 1484511989000,
"name": "Renascence",
"realm": "Vek'nilash",
"battlegroup": "Glutsturm / Emberstorm",
"level": 25,
"side": 1,
"achievementPoints": 2565,
"emblem": {
    "icon": 60,
    "iconColor": "ffb1b8b1",
    "iconColorId": 14,
    "border": 3,
    "borderColor": "ffffffff",
    "borderColorId": 14,
    "backgroundColor": "ff4f2300",
    "backgroundColorId": 44
},
"news": [{
    "type": "itemLoot",
    "character": "Õpiate",
    "timestamp": 1484514300000,
    "itemId": 139074,
    "context": "world-quest-7",
    "bonusLists": [3432, 1502, 3336]
}, {
    "type": "itemLoot",
    "character": "Massaicus",
    "timestamp": 1484514240000,
    "itemId": 124299,
    "context": "raid-mythic",
    "bonusLists": [1799, 1507, 3441]
}, {
    "type": "itemLoot",
    "character": "Druzzen",
    "timestamp": 1484514060000,
    "itemId": 141257,
    "context": "world-quest-9",
    "bonusLists": [3474, 1507, 1674]
}, {
    "type": "itemLoot",
    "character": "Massaicus",
    "timestamp": 1484514000000,
    "itemId": 124187,
    "context": "raid-mythic",
    "bonusLists": [1799, 1522, 3442]
}, {
    "type": "itemLoot",
    "character": "Snippin",
    "timestamp": 1484513820000,
    "itemId": 134316,
    "context": "world-quest-9",
    "bonusLists": [3474, 1517, 3336]
}, {
    "type": "itemLoot",
    "character": "Snippin",
    "timestamp": 1484513580000,
    "itemId": 143684,
    "context": "world-quest-9",
    "bonusLists": [3474, 1512, 3336]
}

1 个答案:

答案 0 :(得分:0)

经典错误;)

if ($type == "itemPurchase"){
...
}

而不是

if ($type = "itemPurchase"){
...
}

... 并且不要忘记,拳头if和second是相同的,也许你应该把弦切换到正确的东西;)