首次发帖并查看/搜索了2天,找到了类似的项目,但找不到嵌套示例,所以希望将其正确格式化为问题。
我从以下文件中获得了测试json:
{
"name": "Cars and Trucks",
"cars": [
{
"cartestvar": "Car Test Var 1",
"carheader": "Car Test Header 1",
"cartypes": [
[
"Ford",
"BMW",
"Fiat"
],
[
"Dodge",
"Lincoln",
"Corvette"
]
]
}
]
}
我希望能够回应出类型的价值观。我理解循环,但foreach得到了我。我试过了:
//$json = file_get_contents('test.json');
$json = file_get_contents('test.json');
//var_dump($json);
$json_a = json_decode($json, true);
$value1 = $json_a['cars'][0].['cartypes'][0][0];
echo '<br/>'. $value1 .'<br/>';
但这没有任何回报。 $ value1测试只是在递归遍历json之前看到1值的第一步。
有关其他参考,请参阅for循环:
if (json_last_error() === JSON_ERROR_NONE) {
//do something with $json. It's ready to use
echo 'got here <br/>';
for($x = 0; $x < 2; $x++) {
for($y = 0; $y < 3; $y++){
//$value1 = $json_a['cartypes'][0][0];
//echo '<br/>'. $value1 .'<br/>';
}
}
} else {
//yep, it's not JSON. Log error or alert someone or do nothing
}