1

I have a xml file with Shows and Seasons in it.

What i'm trying to do is to read out the seasons for one show. But the problem is that each seasons is represented multiple times under the same show.

I only want each season number printed out one time like:

Season 1
Season 2

But what i got now is:

Season 1
Season 2
Season 2
Season 1
Season 1

My xml looks like

<?xml version="1.0"?>
<episodeview>
<episodeview>
    <idShow>1</idShow>
    <idSeason>1</idSeason>
</episodeview>
<episodeview>
    <idShow>1</idShow>
    <idSeason>2</idSeason>
</episodeview>
<episodeview>
    <idShow>1</idShow>
    <idSeason>2</idSeason>
</episodeview>
<episodeview>
    <idShow>1</idShow>
    <idSeason>1</idSeason>
</episodeview>
<episodeview>
    <idShow>1</idShow>
    <idSeason>1</idSeason>
</episodeview>
</episodeview>

And my php file:

<?php
$idShow = "1";
$source = "show.xml";

$xmlstr = file_get_contents($source);
$xmlcont = new SimpleXMLElement($xmlstr);
foreach($xmlcont as $url) {
    if ($url->idShow == $idShow) {
        $test = $url->idSeason;
        echo "Season ";
        echo $test;
        echo "<br>";
     }      
}

?>

2
  • Sounds like dodgy data Commented Feb 26, 2013 at 22:26
  • Yeah the data is not so nice but i have no controll over how the xml file is generated as i get it from an external program. Commented Feb 27, 2013 at 8:41

3 Answers 3

3

try this, short and sweet:

 $xml=simplexml_load_file($source); // (1)

 foreach ($xml->xpath("//idSeason") As $season) { // (2)

      $s[(int)$season]=$season; // (3)
 }

 foreach ($s As $a) echo "Season $a<br />"; // (4)
  1. get a simplexml-Element from the file

  2. select the <idSeason>-nodes only using xpath and iterate through them

  3. create a new array $s with the season-id as index and as value --> an array-index has to be unique, so duplicate ids won't enlarge the array, but "overwrite" the index if it already existed.

  4. iterate through $s to echo it

I am aware that one could select only unique values by xpath, but I'm not that skilled ;-) and simplexml is not supporting xpath 2.0 which is easier.

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

1 Comment

echo 'Season '.implode('<br />Season ', $a); ?
1

I'd go about it like this:

<?php
  $idShow = "1";
  $source = "show.xml";

  $xmlstr  = file_get_contents($source);
  $xmlcont = new SimpleXMLElement($xmlstr);
  $seasons = array();
  foreach($xml_cont as $url){ $seasons[] = $url->idSeason; }

  $seasons = array_uniq($seasons);

  foreach($seasons as $season){ echo "Season $season <br />"; } 
?>

Granted, my example involves more looping than some other solutions you might try, but I'd argue that it's fairly efficient, and perhaps just as importantly, readable.

1 Comment

Thanx i will go for this one. I just added check for show number like foreach($xml_cont as $url){ if ($url->idShow == $idShow) { $seasons[] = $url->idSeason; } }
1

Try:

<?php
$idShow = "1";
$source = "show.xml";
$have = array();

$xmlstr = file_get_contents($source);
$xmlcont = new SimpleXMLElement($xmlstr);
foreach($xmlcont as $url) {
    if ($url->idShow == $idShow) {
        $test = $url->idSeason;
         if( ! in_array( $test, $have) ){ //Check if the season already is displayed
           echo "Season ";
           echo $test;
           echo "<br>";
           $have[] = $test; //Store the season in the array
          }
     }      
}

This way you store everything you displayed in the array, and before you output test, it checkes if it is displayed already.

1 Comment

Thanks for your effort but i can't get it to work its just returns the same as my code.

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.