我有这个JSON数组数据:
[{"region":"Abersee","price":"1.298"},{"region":"Fuschl am See","price":"1.319"},{"region":"St. Gilgen","price":"1.317"},{"region":"Innerschwand","price":"1.321"},{"region":"Mondsee","price":"1.309"},{"region":"Abersee","price":"1.298"},{"region":"Mondsee","price":"1.309"},{"region":"Innerschwand","price":"1.321"},{"region":"Fuschl am See","price":"1.319"},{"region":"St. Gilgen","price":"1.317"},{"region":"Abersee","price":"1.298"},{"region":"Mondsee","price":"1.309"},{"region":"St. Gilgen","price":"1.317"},{"region":"Fuschl am See","price":"1.319"},{"region":"Innerschwand","price":"1.321"},{"region":"Abersee","price":"1.298"},{"region":"St. Gilgen","price":"1.317"},{"region":"Mondsee","price":"1.309"},{"region":"Fuschl am See","price":"1.319"},{"region":"Innerschwand","price":"1.321"},{"region":"Abersee","price":"1.298"},{"region":"Mondsee","price":"1.309"},{"region":"Fuschl am See","price":"1.319"},{"region":"St. Gilgen","price":"1.317"},{"region":"Innerschwand","price":"1.321"},{"region":"Abersee","price":"1.298"},{"region":"Fuschl am See","price":"1.319"},{"region":"Mondsee","price":"1.309"},{"region":"St. Gilgen","price":"1.317"},{"region":"Innerschwand","price":"1.321"}]
我不确定如何正确获取region
和price
数据。我试过这个:
$array = json_encode($jsondata);
$json = json_decode($array, true);
echo $json['region'];
echo $json['price'];
但没有出现。有什么建议吗?
答案 0 :(得分:0)
试试这个:
$array = json_encode($jsondata);
$json = json_decode($array, true);
foreach($json as $data){
echo $data['region'] . "<br>";
echo $data['price'] . "<br>";
}
答案 1 :(得分:0)
正如您所说,json_encode
不是必需的 - 您已经拥有该JSON数据。只需解码它,然后循环它。您当前正在尝试访问第一个条目,但是从JSON到PHP的转换会将数字索引/数组键添加到JSON中不存在的PHP数组中。
foreach
:
$jsondata = '[{"region":"Abersee","price":"1.298"},{"region":"Fuschl am See","price":"1.319"},{"region":"St. Gilgen","price":"1.317"},{"region":"Innerschwand","price":"1.321"},{"region":"Mondsee","price":"1.309"},{"region":"Abersee","price":"1.298"},{"region":"Mondsee","price":"1.309"},{"region":"Innerschwand","price":"1.321"},{"region":"Fuschl am See","price":"1.319"},{"region":"St. Gilgen","price":"1.317"},{"region":"Abersee","price":"1.298"},{"region":"Mondsee","price":"1.309"},{"region":"St. Gilgen","price":"1.317"},{"region":"Fuschl am See","price":"1.319"},{"region":"Innerschwand","price":"1.321"},{"region":"Abersee","price":"1.298"},{"region":"St. Gilgen","price":"1.317"},{"region":"Mondsee","price":"1.309"},{"region":"Fuschl am See","price":"1.319"},{"region":"Innerschwand","price":"1.321"},{"region":"Abersee","price":"1.298"},{"region":"Mondsee","price":"1.309"},{"region":"Fuschl am See","price":"1.319"},{"region":"St. Gilgen","price":"1.317"},{"region":"Innerschwand","price":"1.321"},{"region":"Abersee","price":"1.298"},{"region":"Fuschl am See","price":"1.319"},{"region":"Mondsee","price":"1.309"},{"region":"St. Gilgen","price":"1.317"},{"region":"Innerschwand","price":"1.321"}]';
$json = json_decode($jsondata, true);
foreach($json as $each)
echo $each['region'] . ': ' . $each['price'] . PHP_EOL;
编辑:已解码数组的print_r
(如Marcin Orlowski所述)会在PHP中显示其结构,类似于:
Array
(
[0] => Array
(
[region] => Abersee
[price] => 1.298
)
[1] => Array
(
[region] => Fuschl am See
[price] => 1.319
)