fredix11
fredix11

Reputation: 29

How to skip json object if it has a empty value?

I'm stuck on this since many days... I'm using this code to parse a json EPG guide stored on a server:

<?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>';  
}
?> 

I would like to skip this part of the json from showing because it has empty fields

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

How can I do that?

Upvotes: 0

Views: 725

Answers (1)

Godiez
Godiez

Reputation: 81

You can add in your foreach a condition like:

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

Upvotes: 1

Related Questions