foreach循环,如果这个结果与上一个相同,则跳到不相同的下一个


foreach loop, if this result is same as the last, skip to next that is not the same

我正在运行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

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

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

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

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

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

好吧,这是我选择的。

$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,最后一个时间后面没有逗号,但我不知道该怎么做。我尝试过使用内爆(),但因为每次回显都在if语句中,所以它是一个对象,而不是数组结果。我想。。。我对这个术语不是很熟悉。相反,我选择了每次之前的空格和每次之后的空格。它现在必须工作。:)

感谢大家的帮助!stackoverflow摇滚!!!