A little PHP help required

Thread: A little PHP help required

  1. Tennisball's Avatar

    Tennisball said:

    Default A little PHP help required

    Im not very good with PHP but I decided to try and create a small script that receives a JSON file from a url. I want to split up this data into a simple TV schedule for the current day. I just cant figure out how to select the info I require from the array. Heres my current code, like I said my PHP knowledge is limited, but the task isnt actually that difficult, or so I thought.

    The script should output something like

    Showname - Season Number - Episode Number - Airtime

    I havnt added all the pieces I need to my code yet,, but I cant even get the Episode and Season data. Any help would be massively appreciated.

    Code: [View]
    <?php
    $today = date("Y-m-d");
    $url = "http://api.tvmaze.com/schedule?date=" . $today;
    $json = file_get_contents($url);
    $data = json_decode($json,true);
    $last = count($data) - 1; // might need later
    
    
    foreach ($data as $kkey => $item ) {
            $season = $kkey['season'];
            $ep = $kkey['number'];
            foreach ($kkey['show'] as $key => $value) {
                    $showname = $key['name'];
                    print "Schedule For " . $today . "\r\n";
                    print $showname . " - " . $season . " - " . $ep . "\r\n";
                    }
            }
    
    
    //print"<pre>";
    //print_r($data);
    //print "</pre>";
    ?>
    The commented part at the bottom if uncommented will just print the $data array so you can see the structure of it.
     
  2. Bald Bouncer's Avatar

    Bald Bouncer said:

    Default Re: A little PHP help required

    Code: [View]
    <?php $today = date("Y-m-d");
    $url = "http://api.tvmaze.com/schedule?date=" . $today;
    $json = file_get_contents($url);
    $data = json_decode($json,true);
    $last = count($data) - 1; // might need later
    foreach ($data as $kkey ) {
    $season = $kkey['season'];
    $ep = $kkey['number'];
    $showname = $kkey['show']['name'];
    $episodename = $kkey['name'];
    print "Schedule For " . $today . "\r\n";
    print $showname . " - " . $season . " - " . $ep . " - " . $episodename."\r\n<br>";
    print 'Show Summary - '.$kkey['show']['summary'].'<br>';
    print 'Episode Summary - '.$kkey['summary'].'<br>';
    }
    ?>
    Last edited by Bald Bouncer; 14th November 2015 at 03:46 AM.

    [Only registered and activated users can see links. ]
    For All The Latest Console News, Views & Reviews
    He who sacrifices freedom for security deserves neither - Benjamin Franklin


    [Only registered and activated users can see links. ]
    [Only registered and activated users can see links. ]
     
  3. Tennisball's Avatar

    Tennisball said:

    Default Re: A little PHP help required

    Damn, I was way overthinking it, im sure i had tried the ['key']['item'] thing before, maybe my foreach was wrong.

    Big thanks Bald Bouncer. I owe you a pint.