JSON从字符串php获取数组

时间:2014-09-08 17:14:32

标签: php json

我需要一些关于这个脚本的帮助,它只返回状态,它需要返回showtime和freeseats。

$json_string = 'http://framecreators.nl/efteling/data.php';
$var = json_decode(file_get_contents($json_string), true);
foreach($var['AttractionInfo'] as $info) { 
    if($info['Id'] == 'raveleijn') {
        $informatie = $info['ShowTimes'];
        echo $info['State'];
        break;
    } 
}
echo $informatie;

我的JSON文件= http://framecreators.nl/efteling/data.php

1 个答案:

答案 0 :(得分:0)

$info['ShowTimes']null。试试var_dump($informatie) 试试这个:

$json_string = 'http://framecreators.nl/efteling/data.php';
$var = json_decode(file_get_contents($json_string), true);
foreach($var['AttractionInfo'] as $info) {

    if($info['Id'] == 'raveleijn') {
        $informatie = $info['ShowTimes'];
        echo $info['State'];

        break;
    }

}
var_dump($informatie);

根据要求编辑:

$json_string = 'http://framecreators.nl/efteling/data.php';
$var = json_decode(file_get_contents($json_string), true);
foreach($var['AttractionInfo'] as $info) {

    if($info['Id'] == 'raveleijn') {
        $informatie = $info;


        break;
    }

}
echo is_null($informatie['State'])? 'Not set' : $informatie['State']; 
echo '<br>';
echo is_null($informatie['ShowTimes'])? 'Not set' : $informatie['ShowTimes'];

但是我没有在输入数据中看到“FreeSeats”索引,请检查输入数据