我有一个json输入,简化版本是:
{
"result": {
"status": 1,
"items": [
{
"name": "TF_WEAPON_EXAMPLE",
"defindex": 0,
"craft_material_type": "weapon",
"used_by_classes": [
"Scout",
"Pyro"
]
},
{
"name": "TF_WEAPON_BOTTLE",
"defindex": 1,
"craft_material_type": "weapon",
"used_by_classes": [
"Demoman"
]
},
{
"name": "Paint Can 5",
"defindex": 5031,
"craft_material_type": "tool",
"used_by_classes": [
]
}
]
}
}
我想获取每个“used_by_classes”的值,所以我尝试使用:
$schema_items = $json['result']['items'];
foreach($schema_item['used_by_classes'] as $class)
{
echo $class;
}
这导致我Invalid query: Unknown column 'class' in 'field list'
我该怎么办?如果可以的话,分享一些代码。
谢谢!
答案 0 :(得分:0)
您的代码中存在错误拼写错误
$schema_items = $json['result']['items'];
foreach($schema_item['used_by_classes'] as $class)
//^ here it should be $schema_items
{
echo $class;
}
答案 1 :(得分:0)
这是你应该在这里做的
foreach($item in $schema_items){
echo $item["used_by_classes"]
}
答案 2 :(得分:0)
首先,您在JSON中拥有Object not Array。所以你应该这样做:
$schema_items = $json->result->items;
foreach($schema_items as $class)
{
print_r($class->used_by_classes); // your array with "used_by_classes" values
}
答案 3 :(得分:0)
我通过替换$key => $class
解决了这个问题,如下所示:
foreach($schema_item['used_by_classes'] as $key => $class)
{
echo $class;
}
答案 4 :(得分:0)
以下是解析它的方法:
<强> PHP:强>
$json = json_decode($data, true);
$result = $json['result'];
$schema_items = $result['items'];
foreach($schema_items as $class)
{
foreach($class['used_by_classes'] as $SubClass)
{
echo $SubClass."<br />";
}
}
<强>输出:强>
Scout
Pyro
Demoman