如果它有一个空值,如何跳过json对象?

时间:2017-07-22 17:44:38

标签: php json api if-statement foreach

我很多天都坚持这个......我使用这段代码来解析存储在服务器上的json EPG指南:

<?php
$channel = '899';
$current_unix = time();
$json = json_decode(file_get_contents('http://guidatv.sky.it/app/guidatv/contenuti/data/grid/'.date('y_m_d').'/ch_'.$channel.'.js', true));
echo '<ul>';
foreach ($json->plan as $prog) {
echo "<li>" . $prog->title . "</li>";
echo '</ul>';  
}
?> 

我想跳过这部分json,因为它有空字段

"banned":true,
"plan":[
{"id":"-1",
"pid":"0",
"starttime":"00:00",
"dur":"65",
"title":"",
"normalizedtitle": "",
"desc":"",
"genre":"",
"subgenre":"",
"prima":false 
},

我该怎么做?

1 个答案:

答案 0 :(得分:1)

您可以在foreach中添加以下条件:

foreach ($json->plan as $prog) {
    if(empty($json->plan->title)) continue;
    echo "<li>" . $prog->title . "</li>";
}
echo '</ul>';