foreach循环,如果此结果与最后一个相同,则跳到下一个不一样的

时间:2013-02-16 18:17:36

标签: php object xpath foreach skip

我正在运行一个foreach循环来从xml文件中获取数据。 xml文件具有多次列出的相同日期,每个日期具有不同的数据。我需要做的是只显示一次日期。

基本上我需要foreach循环来显示第一个循环上的第一个日期(对象)。如果日期(对象)在第二,第三,第四循环等上相同,则跳过该循环并移动到日期(对象)不相同的下一个循环。 这就是我现在所拥有的:

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

产生:

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

3 个答案:

答案 0 :(得分:2)

您可以尝试将日期作为键,并检查日期是否已被使用。

$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']

未经测试,如果有什么不起作用,请告诉我。

答案 1 :(得分:0)

您可以将日期添加到数组中,然后在循环期间检查是否存在:

// 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>";
    }
}

答案 2 :(得分:0)

好的,这是我选择的。

$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."- ";
        }
    }
}

产生了这个例子:

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

我使用$ _GET从URL获取日期和短名称,然后我使用短名称来决定我将要处理的xml中的哪个电影。然后我用第一个foreach产生了这个结果。然后我在第一个foreach中运行第二个foreach,专门处理包含日期和时间的子元素。然后我使用if语句根据URL隔离我将要处理的日期。因为if语句,我可以回显该结果中兄弟日期相同的所有时间。我想回应如下的时间:1300,1400,1500,1600,最后一次没有逗号,但我不知道该怎么做。我尝试使用implode(),但因为每次echo都在if语句中,所以它是一个对象而不是数组结果。我认为...我对术语并不十分熟悉。相反,我选择了一个空间 - 在每次之前和之后 - 以及每次之后的空间。它现在必须工作。 :)

谢谢你们所有人的帮助! stackoverflow ROCKS !!!