如何在PHP

时间:2016-01-22 18:55:49

标签: php json

我有一个包含许多层的JSON数组(如果这是正确的名称),我想在PHP中搜索某个位。我已经使用json_decode对其进行了解码,我需要做的是只搜索items部分,但我将通过“defindex”标签进行搜索,它是整个文档的主要ID。我不知道如何运行for循环查看JSON中的多个层。在此先感谢:)

{
"result": {
    "status": 1,
        "qualities": {
        "normal": 0,
        "genuine": 1,
        and so on....
    },
    "originNames": [
        {
            "origin": 0,
            "name": "Timed Drop"
        },
        {
            "origin": 1,
            "name": "Achievement"
        },
            and so on....

    "items": [
        {
            "name": "weapon_deagle",
            "defindex": 1,
            "item_class": "weapon_deagle",
            "item_type_name": "#CSGO_Type_Pistol",
            "item_name": "#SFUI_WPNHUD_DesertEagle",
            "item_description": "#CSGO_Item_Desc_DesertEagle",
            "proper_name": false,
            "item_quality": 0,
            "image_inventory": "econ/weapons/base_weapons/weapon_deagle",
            "min_ilevel": 1,
            "max_ilevel": 1,
            "image_url": "https://steamcdn-a.akamaihd.net/apps/730/icons/econ/weapons/base_weapons/weapon_deagle.29e8f0d7d0be5e737d4f663ee8b394b5c9e00bdd.png",
            "image_url_large": "",
            "craft_class": "weapon",
            "craft_material_type": "weapon",
            "capabilities": {
                "paintable": true,
                "nameable": true,
                "can_sticker": true,
                "can_stattrack_swap": true
            },
            "attributes": [

            ]

        },
        {
            "name": "weapon_elite",
            "defindex": 2,
            "item_class": "weapon_elite",
            "item_type_name": "#CSGO_Type_Pistol",
            "item_name": "#SFUI_WPNHUD_Elites",
            "item_description": "#CSGO_Item_Desc_Elites",
            "proper_name": false,
            "item_quality": 0,
            "image_inventory": "econ/weapons/base_weapons/weapon_elite",
            "min_ilevel": 1,
            "max_ilevel": 1,
            "image_url": "https://steamcdn-a.akamaihd.net/apps/730/icons/econ/weapons/base_weapons/weapon_elite.6563e9d274c6e799d71a7809021624f213d5e080.png",
            "image_url_large": "",
            "craft_class": "weapon",
            "craft_material_type": "weapon",
            "capabilities": {
                "paintable": true,
                "nameable": true,
                "can_sticker": true,
                "can_stattrack_swap": true
            },
            "attributes": [

            ]

        },
        {
            "name": "weapon_fiveseven",
            "defindex": 3,
            "item_class": "weapon_fiveseven",
            "item_type_name": "#CSGO_Type_Pistol",
            "item_name": "#SFUI_WPNHUD_FiveSeven",
            "item_description": "#CSGO_Item_Desc_FiveSeven",
            "proper_name": false,
            "item_quality": 0,
            "image_inventory": "econ/weapons/base_weapons/weapon_fiveseven",
            "min_ilevel": 1,
            "max_ilevel": 1,
            "image_url": "https://steamcdn-a.akamaihd.net/apps/730/icons/econ/weapons/base_weapons/weapon_fiveseven.7c33b4a78ae94a3d14e7cd0f71b295cf61717d75.png",
            "image_url_large": "",
            "craft_class": "weapon",
            "craft_material_type": "weapon",
            "capabilities": {
                "paintable": true,
                "nameable": true,
                "can_sticker": true,
                "can_stattrack_swap": true
            },
            "attributes": [

            ]

        },
        {
            "name": "weapon_glock",
            "defindex": 4,
            "item_class": "weapon_glock",
            "item_type_name": "#CSGO_Type_Pistol",
            "item_name": "#SFUI_WPNHUD_Glock18",
            "item_description": "#CSGO_Item_Desc_Glock18",
            "proper_name": false,
            "item_quality": 0,
            "image_inventory": "econ/weapons/base_weapons/weapon_glock",
            "min_ilevel": 1,
            "max_ilevel": 1,
            "image_url": "https://steamcdn-a.akamaihd.net/apps/730/icons/econ/weapons/base_weapons/weapon_glock.8430afea5349054d0923cefa7d2e7bf3950ce3d7.png",
            "image_url_large": "",
            "craft_class": "weapon",
            "craft_material_type": "weapon",
            "capabilities": {
                "paintable": true,
                "nameable": true,
                "can_sticker": true,
                "can_stattrack_swap": true
            },
            "attributes": [

            ]

        },
        {
            "name": "weapon_ak47",
            "defindex": 7,
            "item_class": "weapon_ak47",
            "item_type_name": "#CSGO_Type_Rifle",
            "item_name": "#SFUI_WPNHUD_AK47",
            "item_description": "#CSGO_Item_Desc_AK47",
            "proper_name": false,
            "item_quality": 0,
            "image_inventory": "econ/weapons/base_weapons/weapon_ak47",
            "min_ilevel": 1,
            "max_ilevel": 1,
            "image_url": "https://steamcdn-a.akamaihd.net/apps/730/icons/econ/weapons/base_weapons/weapon_ak47.a320f13fea4f21d1eb3b46678d6b12e97cbd1052.png",
            "image_url_large": "",
            "craft_class": "weapon",
            "craft_material_type": "weapon",
            "capabilities": {
                "paintable": true,
                "nameable": true,
                "can_sticker": true,
                "can_stattrack_swap": true
            },
            "attributes": [

            ]

        },

1 个答案:

答案 0 :(得分:1)

所以,如果你已经解码了它。您可以轻松地将其视为PHP stdClass

$dataAsObject = json_decode($jsonAsString);

foreach($dataAsObject->result->items as $item) {
   echo $item->defindex . PHP_EOL;
}

或者作为数组,如果您为true的第二个参数传递json_decode

$dataAsArray = json_decode($jsonAsString, true);

foreach($dataAsArray['result']['items'] as $item) {
   echo $item['defindex'] . PHP_EOL;
}

listen中的更多信息。