首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通过json阵列进行过滤

通过json阵列进行过滤
EN

Stack Overflow用户
提问于 2016-02-03 02:22:20
回答 2查看 123关注 0票数 0

它代表的是来自蒸汽的贸易报价。每个报价都有"items_to_give“和"items_to_receive”。

我希望这个json数组看起来像这样

代码语言:javascript
运行
复制
{
"items_to_give": [
                {
                    "market_hash_name": "AWP | Asiimov (Field-Tested)",
                },
                {
                    "market_hash_name": "M4A4 | Asiimov (Field-Tested)"  
                }
                 ]
            ,

"items_to_receive": [
                {
                    "market_hash_name": "★ Falchion Knife | Blue Steel (Field-Tested)"
                }                      
                    ]
}

每个项目都有独特的“分类”和"instanceid“,但是它们的"market_hash_name”显示在数组的“描述”部分。

首先,我必须找到他们的名字使用“分类”和"instanceid“,而不是移动到"items_to_give”和"items_to_receive“。

这是我的意思的照片。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-02-03 04:06:09

只需制作一个文件并测试它。我觉得你能做到。

结果如下:

代码语言:javascript
运行
复制
{
    "items_to_give": [
        {
            "instanceid": "480085569",
            "classid": "1440311074",
            "assetid": "5003822520",
            "amount": "1",
            "market_hash_name": "AWP | Asiimov (Field-Tested)",
            "market_name": "AWP | Asiimov (Field-Tested)",
            "name": "AWP | Asiimov",
            "icon_url": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot621FAR17PLfYQJD_9W7m5a0mvLwOq7c2G9SupUijOjAotyg3w2x_0ZkZ2rzd4OXdgRoYQuE8gDtyL_mg5K4tJ7XiSw0WqKv8kM",
            "action_link": "steam:\/\/rungame\/730\/76561202255233023\/+csgo_econ_action_preview%20S%owner_steamid%A%assetid%D9394689721994738281"
        },
        {
            "instanceid": "188530139",
            "classid": "310777979",
            "assetid": "4999225449",
            "amount": "1",
            "market_hash_name": "M4A4 | Asiimov (Field-Tested)",
            "market_name": "M4A4 | Asiimov (Field-Tested)",
            "name": "M4A4 | Asiimov",
            "icon_url": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpou-6kejhjxszFJQJD_9W7m5a0mvLwOq7c2GlUucFwjruR9t7231DmrRc_NW7yItCRcVNoYVHS-APrwbzu0JK-78nXiSw0EnwDRM0",
            "action_link": "steam:\/\/rungame\/730\/76561202255233023\/+csgo_econ_action_preview%20S%owner_steamid%A%assetid%D459488816407887944"
        }
    ],
    "items_to_receive": [
        {
            "instanceid": "188530139",
            "classid": "1015898744",
            "assetid": "5004700589",
            "amount": "1",
            "market_hash_name": "\u2605 Falchion Knife | Blue Steel (Field-Tested)",
            "market_name": "\u2605 Falchion Knife | Blue Steel (Field-Tested)",
            "name": "\u2605 Falchion Knife | Blue Steel",
            "icon_url": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpovbSsLQJf1fLEcjVL49KJlZG0lfvhNr_um25V4dB8xOrApdqmjAbi_hFtYGr7cICUdVVtNA6D81m9yObr0cDotJnJnSM3viU8pSGKmL5fGRg",
            "action_link": "steam:\/\/rungame\/730\/76561202255233023\/+csgo_econ_action_preview%20S%owner_steamid%A%assetid%D14288283571522546212"
        }
    ]
}

我还在这段代码中添加了一些所需的内容。因为我也是蒸汽使用者。

代码语言:javascript
运行
复制
<?php
$steamResponse = '{
    "response": {
        "trade_offers_received": [
            {
                "tradeofferid": "993371789",
                "accountid_other": 254083372,
                "message": "I just got a fantastic idea. This could make everything good between us.          ;)",
                "expiration_time": 1455660736,
                "trade_offer_state": 7,
                "items_to_give": [
                    {
                        "appid": "730",
                        "contextid": "2",
                        "assetid": "5003822520",
                        "classid": "1440311074",
                        "instanceid": "480085569",
                        "amount": "1",
                        "missing": false
                    },
                    {
                        "appid": "730",
                        "contextid": "2",
                        "assetid": "4999225449",
                        "classid": "310777979",
                        "instanceid": "188530139",
                        "amount": "1",
                        "missing": false
                    }
                ]
                ,
                "items_to_receive": [
                    {
                        "appid": "730",
                        "contextid": "2",
                        "assetid": "5004700589",
                        "classid": "1015898744",
                        "instanceid": "188530139",
                        "amount": "1",
                        "missing": false
                    }
                ]
                ,
                "is_our_offer": false,
                "time_created": 1454451136,
                "time_updated": 1454451250,
                "from_real_time_trade": false,
                "escrow_end_date": 0,
                "confirmation_method": 0
            }
        ]
        ,
        "descriptions": [
            {
                "appid": 730,
                "classid": "1440311074",
                "instanceid": "480085569",
                "currency": false,
                "background_color": "",
                "icon_url": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot621FAR17PLfYQJD_9W7m5a0mvLwOq7c2G9SupUijOjAotyg3w2x_0ZkZ2rzd4OXdgRoYQuE8gDtyL_mg5K4tJ7XiSw0WqKv8kM",
                "icon_url_large": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot621FAR17PLfYQJD_9W7m5a0mvLwOq7cqWdQ-sJ0xOzAot-jiQa3-hBqYzvzLdSVJlQ3NQvR-FfsxL3qh5e7vM6bzSA26Sg8pSGKJUPeNtY",
                "descriptions": [
                    {
                        "type": "html",
                        "value": "Exterior: Field-Tested"
                    },
                    {
                        "type": "html",
                        "value": " "
                    },
                    {
                        "type": "html",
                        "value": "High risk and high reward, the infamous AWP is recognizable by its signature report and one-shot, one-kill policy. It has been custom painted with a sci-fi design.\n\n<i>Anyone can predict the future... a visionary shapes it</i>"
                    },
                    {
                        "type": "html",
                        "value": " "
                    },
                    {
                        "type": "html",
                        "value": "The Phoenix Collection",
                        "color": "9da1a9"
                    },
                    {
                        "type": "html",
                        "value": " "
                    },
                    {
                        "type": "html",
                        "value": "<br><div id=\"sticker_info\" name=\"sticker_info\" title=\"Sticker Details\" style=\"border: 2px solid rgb(102, 102, 102); border-radius: 6px; width=100; margin:4px; padding:8px;\"><center><img width=64 height=48 src=\"https://steamcdn-a.akamaihd.net/apps/730/icons/econ/stickers/team_roles_capsule/baiter.f14ba197e75a0b0074c74e7f98af9eb4d111981b.png\"><img width=64 height=48 src=\"https://steamcdn-a.akamaihd.net/apps/730/icons/econ/stickers/team_roles_capsule/lurker.850f26af6a1e55f4ed398da610d9ceb73ce0b338.png\"><img width=64 height=48 src=\"https://steamcdn-a.akamaihd.net/apps/730/icons/econ/stickers/team_roles_capsule/awper.251e754fcfd1d398fcedc220dc44f479b841c8ea.png\"><br>Sticker: The Baiter, The Lurker, The Awper</center></div>"
                    }
                ]
                ,
                "tradable": true,
                "actions": [
                    {
                        "link": "steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20S%owner_steamid%A%assetid%D9394689721994738281",
                        "name": "Inspect in Game..."
                    }
                ]
                ,
                "name": "AWP | Asiimov",
                "name_color": "D2D2D2",
                "type": "Covert Sniper Rifle",
                "market_name": "AWP | Asiimov (Field-Tested)",
                "market_hash_name": "AWP | Asiimov (Field-Tested)",
                "market_actions": [
                    {
                        "link": "steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M%listingid%A%assetid%D9394689721994738281",
                        "name": "Inspect in Game..."
                    }
                ]
                ,
                "commodity": false,
                "market_tradable_restriction": 7
            },
            {
                "appid": 730,
                "classid": "310777979",
                "instanceid": "188530139",
                "currency": false,
                "background_color": "",
                "icon_url": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpou-6kejhjxszFJQJD_9W7m5a0mvLwOq7c2GlUucFwjruR9t7231DmrRc_NW7yItCRcVNoYVHS-APrwbzu0JK-78nXiSw0EnwDRM0",
                "icon_url_large": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpou-6kejhjxszFJQJD_9W7m5a0mvLwOq7cqWdQ-sJ0xOvEpIj0jAbkqEE_ZD3xctLGJAE_Zw7U-QTowefth8TpvM_InHZh6XQ8pSGKWYJAoJI",
                "descriptions": [
                    {
                        "type": "html",
                        "value": "Exterior: Field-Tested"
                    },
                    {
                        "type": "html",
                        "value": " "
                    },
                    {
                        "type": "html",
                        "value": "More accurate but less damaging than its AK-47 counterpart, the M4A4 is the full-auto assault rifle of choice for CTs. It has been custom painted with a sci-fi design.\n\n<i>Anyone can predict the future... a visionary shapes it</i>"
                    },
                    {
                        "type": "html",
                        "value": " "
                    },
                    {
                        "type": "html",
                        "value": "The Winter Offensive Collection",
                        "color": "9da1a9"
                    },
                    {
                        "type": "html",
                        "value": " "
                    }
                ]
                ,
                "tradable": true,
                "actions": [
                    {
                        "link": "steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20S%owner_steamid%A%assetid%D459488816407887944",
                        "name": "Inspect in Game..."
                    }
                ]
                ,
                "name": "M4A4 | Asiimov",
                "name_color": "D2D2D2",
                "type": "Covert Rifle",
                "market_name": "M4A4 | Asiimov (Field-Tested)",
                "market_hash_name": "M4A4 | Asiimov (Field-Tested)",
                "market_actions": [
                    {
                        "link": "steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M%listingid%A%assetid%D459488816407887944",
                        "name": "Inspect in Game..."
                    }
                ]
                ,
                "commodity": false,
                "market_tradable_restriction": 7
            },
            {
                "appid": 730,
                "classid": "1015898744",
                "instanceid": "188530139",
                "currency": false,
                "background_color": "",
                "icon_url": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpovbSsLQJf1fLEcjVL49KJlZG0lfvhNr_um25V4dB8xOrApdqmjAbi_hFtYGr7cICUdVVtNA6D81m9yObr0cDotJnJnSM3viU8pSGKmL5fGRg",
                "icon_url_large": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpovbSsLQJf1fLEcjVL49KJlZG0lfvhNr_um25V4dB8teXA54vwxle1-0VvN2v1IdSUcQU2NV7R_QLtlLi8jJ6_tZXNnSQ3siQisS6JmxCpwUYbIOval6A",
                "descriptions": [
                    {
                        "type": "html",
                        "value": "Exterior: Field-Tested"
                    },
                    {
                        "type": "html",
                        "value": " "
                    },
                    {
                        "type": "html",
                        "value": "A modern homage to a falchion sword, this clip point blade has a curved edge. It has been cold blued.\n\n<i>This is the malbec of weapon design - Booth, Arms Dealer</i>"
                    },
                    {
                        "type": "html",
                        "value": " "
                    }
                ]
                ,
                "tradable": true,
                "actions": [
                    {
                        "link": "steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20S%owner_steamid%A%assetid%D14288283571522546212",
                        "name": "Inspect in Game..."
                    }
                ]
                ,
                "name": "★ Falchion Knife | Blue Steel",
                "name_color": "8650AC",
                "type": "★ Covert Knife",
                "market_name": "★ Falchion Knife | Blue Steel (Field-Tested)",
                "market_hash_name": "★ Falchion Knife | Blue Steel (Field-Tested)",
                "market_actions": [
                    {
                        "link": "steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M%listingid%A%assetid%D14288283571522546212",
                        "name": "Inspect in Game..."
                    }
                ]
                ,
                "commodity": false,
                "market_tradable_restriction": 7
            }
        ]

    }
}';


$json = json_decode($steamResponse); // $steamResponse is the json response from steam

$result = array();
$result['items_to_give'] = array();
$result['items_to_receive'] = array();


foreach($json->response->trade_offers_received[0]->items_to_give as $key => $value){
    $result['items_to_give'][$key]['instanceid'] = $value->instanceid;
    $result['items_to_give'][$key]['classid'] = $value->classid;
    $result['items_to_give'][$key]['assetid'] = $value->assetid;
    $result['items_to_give'][$key]['amount'] = $value->amount;

    foreach($json->response->descriptions as $key2 => $value2){
        if(($value2->instanceid == $value->instanceid) && ($value2->classid == $value->classid)){
            $result['items_to_give'][$key]['market_hash_name'] = $value2->market_hash_name;
            $result['items_to_give'][$key]['market_name'] = $value2->market_name;
            $result['items_to_give'][$key]['name'] = $value2->name;
            $result['items_to_give'][$key]['icon_url'] = $value2->icon_url;
            $result['items_to_give'][$key]['action_link'] = $value2->actions[0]->link;
        }
    }
}

foreach($json->response->trade_offers_received[0]->items_to_receive as $key => $value){
    $result['items_to_receive'][$key]['instanceid'] = $value->instanceid;
    $result['items_to_receive'][$key]['classid'] = $value->classid;
    $result['items_to_receive'][$key]['assetid'] = $value->assetid;
    $result['items_to_receive'][$key]['amount'] = $value->amount;
    foreach($json->response->descriptions as $key2 => $value2){
        if(($value2->instanceid == $value->instanceid) && ($value2->classid == $value->classid)){
            $result['items_to_receive'][$key]['market_hash_name'] = $value2->market_hash_name;
            $result['items_to_receive'][$key]['market_name'] = $value2->market_name;
            $result['items_to_receive'][$key]['name'] = $value2->name;
            $result['items_to_receive'][$key]['icon_url'] = $value2->icon_url;
            $result['items_to_receive'][$key]['action_link'] = $value2->actions[0]->link;
        }
    }
}


echo '<pre>';
print_r(json_encode($result, JSON_PRETTY_PRINT));
echo '</pre>';
票数 0
EN

Stack Overflow用户

发布于 2016-02-03 06:44:21

首先将JSON转换为PHP

代码语言:javascript
运行
复制
$response = json_decode($json, true);

遍历嵌套数据的一个好方法是使用PHP的array_*函数。这就是为什么我转换为关联数组而不是对象的原因。

现在创建一个函数,通过给予或接收来获取ID。

代码语言:javascript
运行
复制
function classIds($index, $response)
{
    return array_map(function ($item) { return $item["classid"]; }, $response["response"]["trade_offers_received"][0]["items_to_" . $index]);
}

和另一个函数来获取这些ID的描述:

代码语言:javascript
运行
复制
function descriptionForIds($ids, $response)
{
    return array_map(
        // After finding matching IDs return their market hash name
        function ($description) {
            return [ "market_hash_name" => $description["market_hash_name"] ];
        },
        // Find matching IDs
        array_filter(
            $response["response"]["descriptions"],
            function ($description) use ($ids) { return in_array($description["classid"], $ids); }
        )
    );
}

使用以下函数构建结果数组:

代码语言:javascript
运行
复制
$result = [
    "items_to_give" => descriptionForIds(classIds("give", $response), $response),
    "items_to_receive" => descriptionForIds(classIds("receive", $response), $response),
];

然后回到JSON:

代码语言:javascript
运行
复制
json_encode($result);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35167429

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档