解析返回空值

问题描述 投票:0回答:1

我在Steam服务中对DotA 2用户清单中的项目进行解析。每次尝试解析用户数据时,我都会得到一个空值:

{"success":true,"items":[]},但我的Steam库存中有物品。

我解析项目的功能:

public function loadMyInventory() {
    if(Auth::guest()) return ['success' => false];
    $prices = json_decode(Storage::get('prices.txt'), true);
    $response = json_decode(file_get_contents('https://steamcommunity.com/inventory/'.$this->user->steamid64.'/570/2?l=russian&count=5000'), true);
    if(time() < (Session::get('InvUPD') + 5)) {
        return [
            'success' => false,
            'msg' => 'Error, repeat in '.(Session::get('InvUPD') - time() + 5).' сек.',
            'status' => 'error'
        ];
    }
    //return $response;
    $inventory = [];

    foreach($response['assets'] as $item) {
        $find = 0;
        foreach($response['descriptions'] as $descriptions) {
            if($find == 0) {
                if(($descriptions['classid'] == $item['classid']) && ($descriptions['instanceid'] == $item['instanceid'])) {
                    $find++;
                    # If we find the price of an item, then move on.
                    if(isset($prices[$descriptions['market_hash_name']])) {
                        # Search data
                        $price = $prices[$descriptions['market_hash_name']]*$this->config->curs;
                        $class = false;
                        $text = false;

                        if($price <= $this->config->min_dep_sum) {
                            $price = 0;
                            $text = 'Cheap';
                            $class = 'minPrice';
                        }

                        if(($descriptions['tradable'] == 0) || ($descriptions['marketable'] == 0)) {
                            $price = 0;
                            $class = 'minPrice';
                                $text = 'Not tradable';
                        }
                        # Adding to Array
                        $inventory[] = [
                            'name' => $descriptions['market_name'],
                            'price' => floor($price),
                            'color' => $this->getRarity($descriptions['tags']),
                            'tradable' => $descriptions['tradable'],
                            'class' => $class,
                            'text' => $text,
                            'classid' => $item['classid'],
                            'assetid' => $item['assetid'],
                            'instanceid' => $item['instanceid']
                        ];   
                    }
                }
            }
        }
    }
    Session::put('InvUPD', (time() + 5));
    return [
        'success' => true,
        'items' => $inventory
    ];
}

但是应该返回大约以下值:

{"success":true,"items":[{"classid":"2274725521","instanceid":"57949762","assetid":"18235196074","market_hash_name":"Full-Bore Bonanza","price":26}]}

我的错误在哪里?

php json
1个回答
0
投票

首先,您要遍历每个资产的描述,这是资产*描述迭代,虽然很多,但是您可以对其进行优化。

让我们循环一次以进行描述,并将classid和instanceid分配为对象键。

$assets = $response["assets"];
$descriptions = $response["descriptions"];

$newDescriptions=[]; 
foreach($descriptions as $d){
    $newDescriptions[$d["classid"]][$d["instanceid"]] = $d;
}

这将提供每次都不会遍历描述的功能,我们可以直接访问某些资产的描述$ newDescriptions [$ classid] [$ instanceid]]

foreach($assets as $a){
    if(isset($newDescriptions[$a["classid"]]) && isset($newDescriptions[$a["classid"]][$a["instanceid"]])){
            $assetDescription = $newDescriptions[$a["classid"]][$a["instanceid"]];
            if($prices[$assetDescription["market_hash_name"]]){
                print_r($assetDescription);    
            }
    }
}

关于您的错误:

您确定您的“ prices.txt”包含market_hash_name吗?

我还没有看到任何其他问题,对您在注释中提供的数据进行操作,我得到了变量$ assetDescription的打印。请仔细检查变量$ prices。

© www.soinside.com 2019 - 2024. All rights reserved.