Question

i am running a foreach loop to get data from an xml file. the xml file has the same date listed several times, each with different data. what i need to do is show each date only once.

basicly i need the foreach loop to show the first date (object) on the first loop. if the date (object) is the same on the second, third, fourth loop, etc. then skip that loop and move to the next where the date (object) is not the same. here is what i have now:

$dateResults = $xml->xpath('/rtnshowtime/filmtitle/show[preceding-sibling::shortname="AGOODDAYTODIEHARD"]');
foreach ($dateResults as $dateResult) {
    print_r($dateResult->date);
    echo "<br>";
}

that produces:

SimpleXMLElement Object ( [0] => 02152013 ) 
SimpleXMLElement Object ( [0] => 02152013 ) <-- this one needs to be skipped
SimpleXMLElement Object ( [0] => 02152013 ) <-- this one needs to be skipped
SimpleXMLElement Object ( [0] => 02162013 ) 
SimpleXMLElement Object ( [0] => 02162013 ) <-- this one needs to be skipped
SimpleXMLElement Object ( [0] => 02162013 ) <-- this one needs to be skipped
SimpleXMLElement Object ( [0] => 02162013 ) <-- this one needs to be skipped
SimpleXMLElement Object ( [0] => 02162013 ) <-- this one needs to be skipped
SimpleXMLElement Object ( [0] => 02172013 ) 
SimpleXMLElement Object ( [0] => 02172013 ) <-- this one needs to be skipped
SimpleXMLElement Object ( [0] => 02172013 ) <-- this one needs to be skipped
SimpleXMLElement Object ( [0] => 02172013 ) <-- this one needs to be skipped
SimpleXMLElement Object ( [0] => 02172013 ) <-- this one needs to be skipped
SimpleXMLElement Object ( [0] => 02182013 ) 
SimpleXMLElement Object ( [0] => 02182013 ) <-- this one needs to be skipped
SimpleXMLElement Object ( [0] => 02192013 ) 
SimpleXMLElement Object ( [0] => 02192013 ) <-- this one needs to be skipped
SimpleXMLElement Object ( [0] => 02202013 ) 
SimpleXMLElement Object ( [0] => 02202013 ) <-- this one needs to be skipped
SimpleXMLElement Object ( [0] => 02212013 ) 
SimpleXMLElement Object ( [0] => 02212013 ) <-- this one needs to be skipped
Was it helpful?

Solution 3

ok, here's what i have opted for.

$shortname = $_GET['shortname'];
$dateURL = $_GET['date'];
$use_errors = libxml_use_internal_errors(true);
$xml = simplexml_load_file('XML/showtimes.xml');
if (!$xml) {echo "NO XML loaded<br>";}else{echo "XML IS loaded<br>";}
$results = $xml->xpath('/rtnshowtime/filmtitle[child::shortname="'.$shortname.'"]');

foreach ($results as $result) {
    echo "showtimes for ".$dateURL."<br>";
    foreach ($result->show as $result2) {
        if ($result2->date == $dateURL) {
            echo " -".$result2->time."- ";
        }
    }
}

that produces this for example:

showtimes for 02152013
 -1300-  -1400-  -1500-  -1600-  -1700- 

i use $_GET to get the date and shortname from the URL, then i use the shortname to decide which movie inside the xml that i will be dealing with. i then produce that as a result with the first foreach. i then run a second foreach within the first foreach specifically dealing with the child element that contains the dates and times. i then use an if statement to segregate which date i will be dealing with based on the URL. because of that if statement, i can then echo all of the times within that result where the sibling dates are the same. i would like to echo the times such as: 1300, 1400, 1500, 1600 with no comma following the last time, but i dont know how to do that. i tried using implode(), but because each time echo is inside an if statement it's an object instead of array results. i assume that... i'm not extremely familiar with the terminology. i have instead opted for a space and - before each time and a - and space after each time. it will have to work for now. :)

thanks to all of you guys for your assistance! stackoverflow ROCKS!!!

OTHER TIPS

You could try something like putting the date as key and check to see if the date has already been used.

$dateResults = $xml->xpath('/rtnshowtime/filmtitle/show[preceding-sibling::shortname="AGOODDAYTODIEHARD"]');

$finalResults = array();

foreach ($dateResults as $dateResult) {

    // change your simplexml object to either (int) or (string)
    $date = (int) $dateResult->date;

    // checks key for repeating date
    if (!array_key_exists($date, $finalResults)){
        // assuming you want the entire $dateResult match with date as key
        $finalResults[$date] = $dateResult
    }       

}

//  to print out the results
foreach ( $finalResult as $result ){
    foreach ( $results as $key => $value ){
        echo $key." : ".$value;
    }
}

// or if you know the date and what you want from that array
echo (string) $finalResult[2152013]['salelink']

Untested, please let me know if theres something not working.

You could add the dates into an array and then check for existence during the loop:

// create array for storing unique dates
$unique_dates = array();

$dateResults = $xml->xpath('/rtnshowtime/filmtitle/show[preceding-sibling::shortname="AGOODDAYTODIEHARD"]');
foreach ($dateResults as $dateResult) 
{
    // need this to get the time attribute from the object
    $time_index = 0;

    // if the date does not exist in unique dates array
    if ( ! in_array($dateResult->date->$time_index, $unique_dates))
    {
        // add to unique dates array
        $unique_dates[] = $dateResult->date->$time_index;

        print_r($dateResult->date);
        echo "<br>";
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top