JSON从字符串php获取数组
我需要一些关于这个脚本的帮助,它只返回状态,并且需要返回showtime和freeseatJSON从字符串php获取数组,php,json,Php,Json,我需要一些关于这个脚本的帮助,它只返回状态,并且需要返回showtime和freeseat $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') {
$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文件=的
$info['ShowTimes']
为null
。尝试var\u 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'];
$json\u string=>http://framecreators.nl/efteling/data.php';
$var=json\u decode(file\u get\u contents($json\u string),true);
foreach($var['AttractionInfo']作为$info){
如果($info['Id']=='raveleijn'){
$informatie=$info;
打破
}
}
echo为空($informatie['State'])?'未设置“:$informatie['State'];
回声“
”;
echo为空($informatie['ShowTimes'])?'未设置“:$informatie['ShowTimes'”;
但是我没有在您的输入数据中看到“FreeSeats”索引,请检查您的输入数据请将所有相关代码和示例数据内联在问题中,而不是作为链接。显示时间为空。不适用于id Raveleijn:)请检查{“id”:“Raveleijn”,“Type”:“Show”,“MapLocation”:“7”,“WaitingTime”:NULL,“OpeningHours”:NULL,“State”:“gesloten”,“StateColor”:“clear”,“StatePercentage”:null,“ShowTimes”:null,“HorecaTimes”:null}Omg谢谢。但是如何获取var ShowDateTime和freeseat?@MickGeen ShowDateTime和freeseat不在json中。请将其粘贴到jsonlint.com或在浏览器中安装json漂亮打印扩展以更好地查看json数据。@JimL是的,您应该检查输入数据。