1

Background: I am pulling in XML objects from a public IMDB-for-TV API. My goal is to pull certain values out of those objects -- like, say, a list of every episode title, put into an array so that I can do what I want with it.

Problem: Although I can write code that does execute exactly the way I want it, it spits out errors as it does so, so I know something's wrong with the way I iterate over my objects... but I can't figure out a better way. I'd really love some advice.

So first off, here's a look at the object I'm dealing with.

SimpleXMLElement Object
(
    [Episodelist] => SimpleXMLElement Object
        (
            [Season] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [episode] => Array
                                (
                                    [0] => SimpleXMLElement Object
                                        (
                                            [epnum] => 1
                                            [seasonnum] => 01
                                            [prodnum] => 101
                                            [airdate] => 1989-07-05
                                            [link] => http://www.tvrage.com/Seinfeld/episodes/305788
                                            [title] => Good News, Bad News
                                        )

                                    [1] => SimpleXMLElement Object
                                        (
                                            [epnum] => 2
                                            [seasonnum] => 02
                                            [prodnum] => 103
                                            [airdate] => 1990-05-31
                                            [link] => http://www.tvrage.com/Seinfeld/episodes/150618
                                            [title] => The Stakeout
                                        )

The show is an object containing an object "episode list", which contains object "season x", which contains object "episode y", which contains the value I'm after -- "title". So for each episode, I want to grab $this->list->Season[X]->episode[Y]->title.

Here is the function I wrote to do this. It takes two arguments: the season number, and the episode number.

 public function showEpisode($s,$e) {
    $s = $s - 1; // To compensate for zero-indexing.
    $e = $e - 1; 
    if (!empty($this->list->Season[$s]) && !empty($this->list->Season[$s]->episode[$e])) {
        return $this->list->Season[$s]->episode[$e]->title;     
    } else {
        return 0;
    }

}

I know there's something wrong with how it's written.

Anyway, here's my code for actually working with it.

 $season = 1;
 $episode = 1;
 $errors = 0;
 while ($errors < 2) {
if ($xfiles->showEpisode($season,$episode)!= 0) {
    echo $xfiles->showEpisode($season,$episode) . "<br />"; 
    $episode++;
    $errors = 0;
} else {
    $errors++;
    $season++;
    $episode = 1;
}
 }

My logic is:

  1. Start at Season 1, Episode 1.
  2. Echo the title of the current episode.
  3. If that fails, increment the error-counter by one, and go up a season by incrementing $season++ and putting the $episode counter back at 1.
  4. If you get two errors in a row, it means going up a season failed, because we've hit the end of the show, so the loop ends.

Desired result: A neat, simple list of every episode title. Actual result: A blank page, using this code; nothing is ever returned. When I used my last version of the function, which I have very stupidly deleted and cannot seem to recreate, I did echo a full set of episodes exactly as I wanted -- but after each season, and three times at the end of the file, I got "Trying to get property of non-object" errors, because of the calls to non-existent episodes.

Question: What is the correct, practical way to loop through a large object like this? What conditions should I use in my showEpisode function to check if the result will exist or not?

Thanks a ton to anyone who can help, I've done my best and Googled a lot but I'm just baffled.

1
  • +1, this is the best-written question I have ever seen! I'm really glad I could help :) Commented Jan 29, 2014 at 14:12

1 Answer 1

2

This looks like a job for a foreach-loop.

foreach ($xfiles->list->Season as $season) {
    foreach ($season->episode as $episode) {
        echo $episode->title . "<br />"; 
    }
}

Alternatively (or should I say ideally), put this inside a method of the list object and replace $xfiles with $this.

Sign up to request clarification or add additional context in comments.

1 Comment

Well... damn. That works absolutely perfectly. Such a simple and elegant solution, I never thought of doing something like that. Thanks a ton, I appreciate the advice and I actually learnt something from it!

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.