0

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

3 回答 3

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

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

于 2013-02-16T18:24:49.403 回答
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>";
    }
}
于 2013-02-16T18:26:30.247 回答
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(),但因为每次回显都在 if 语句中,它是一个对象而不是数组结果。我假设...我' m 对术语不是很熟悉。相反,我选择了一个空格和 - 每次之前和 - 每次之后都有空格。它现在必须工作。:)

感谢大家的帮助!堆栈溢出摇滚!

于 2013-02-27T02:56:24.730 回答