0

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?

4
  • based on which empty element basically you want to skip? title, desc, or any-one else? Commented Jul 22, 2017 at 17:46
  • I want to skip empty title Commented Jul 22, 2017 at 17:53
  • @YvesLeBorg What about $plan->title = "" ? that will pass the condition Commented Jul 22, 2017 at 18:02
  • Get used to good habits. do echo "<li>" . htmlspecialchars($prog->title) . "</li>"; instead of echo "<li>" . $prog->title . "</li>"; . to protect yourself from HTML injection. Commented Jul 22, 2017 at 18:07

1 Answer 1

1

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>';  
Sign up to request clarification or add additional context in comments.

3 Comments

Be careful with the "0" problem, if the title is "0" it will not be echoed. also remove the </ul> from the loop
it works but the first entry has a strange indentation that I can't remove: phpfiddle.org/lite/code/i765-2ajn
As Accountant said, </ul> must be outside, I updated my answer, check it out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.