PHP - 变量返回NULL,但它不应该?

时间:2016-05-04 19:15:48

标签: php json

所以我想尝试制作,代码将获得与JSON数组中的ID匹配的某些部分。

NULL

这是JSON:http://pastebin.ca/3591035

$ ItemName返回print(pandas.pivot_table(dataframe,values='id',index=['major_one','major_two'],columns='status',aggfunc='count',fill_value=fill_character),'\n') 但它不应该(至少我认为)。也许有人可以发现我在这里做的错误:/

2 个答案:

答案 0 :(得分:0)

第二个参数truejson_decode告诉它将JSON对象转换为PHP关联数组而不是PHP对象。但是使用像$json->rgDescriptions这样的语法需要$json作为对象;对于数组,它应该是$json['rgDescriptions']

因此,要么将$json的所有用法更改为使用数组语法,要么从true中删除json_decode参数。后者应该更容易。

此外,这一行:

$invIndexes = $index;

应该是:

$invIndexes[] = $index;

但你可以用:

代替那个循环
$invIndexes = $json->rgInventory;

答案 1 :(得分:0)

如果您在json decode $json = json_decode($response, true);中使用true,那么它将返回一个关联数组,因此您可以像$json['rgInventory']而不是$json->rgInventory一样访问值表单数组

要创建$invIndexes数组,请使用以下命令:

$invIndexes = array();
foreach($json['rgInventory'] as $index){
    $invIndexes[] = $index['id'];
}

在这里,您将获得$invIndexes在您的厕所中,您再次使用$json->rgDescriptions来访问值,将其更改为$json['rgInventory'],并且对于所有其他值,使用数组键,例如$json['rgInventory']['index']['class'] 1}}

无需$makearray = (array)$invIndexes;直接使用$invIndexes

$index = $invIndexes[$id];
$item = $json['rgDescriptions'][$json['rgInventory'][$index]['classid']."_".$json['rgInventory'][$index]['instanceid']];

另一个错误是,在您的$ item中没有任何键tradeable,其tradable就像这样使用

if($item['tradeable'] != 1){
    continue;
}

 $ItemName = $item['market_hash_name'];

最后var_dump($ItemName);

相关问题