【问题标题】:foreach loop, if this result is same as the last, skip to next that is not the sameforeach循环,如果这个结果和上一个结果一样,跳到下一个不一样的地方
【发布时间】:2013-02-16 18:17:35
【问题描述】:

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

【问题讨论】:

    标签: php object xpath foreach skip


    【解决方案1】:

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

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

    未经测试,如果有问题请告诉我。

    【讨论】:

    • 它返回: 注意:未定义变量:C:\UniServer\UniServer\www\boaz9REDUX\xmlreadertest.php 中的日期第 28 行 注意:未定义变量:C:\UniServer\UniServer\ 中的 finalResults第 29 行的 www\boaz9REDUX\xmlreadertest.php 警告:array_key_exists() 期望参数 2 为数组,在第 29 行的 C:\UniServer\UniServer\www\boaz9REDUX\xmlreadertest.php 中给出 null 警告:C 中的非法偏移类型: \UniServer\UniServer\www\boaz9REDUX\xmlreadertest.php 在第 30 行
    • 我对php的了解非常原始。我无法解释循环、数组、对象等,但我知道它们是不同的。当我尝试 array_unique 时,它​​基本上说我没有输出数组。
    • 我修复了 $date = $dateResult-&gt;$date(未定义变量:日期)到 $date = $dateResult-&gt;date 的语法错误。确保在循环之前添加 $finalResults = array();。警告是因为 $finalResults 现在正在阅读 NULL
    • 警告:array_key_exists():第一个参数应该是 C:\UniServer\UniServer\www\boaz9REDUX\xmlreadertest.php 中第 33 行的字符串或整数警告:C 中的非法偏移类型:\UniServer\UniServer\www\boaz9REDUX\xmlreadertest.php 第 35 行
    • 第 35 行是:$finalResults[$date] = $dateResult;
    【解决方案2】:

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

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

    【讨论】:

    • 产生这个:注意:未定义的变量:C:\UniServer\UniServer\www\boaz9REDUX\xmlreadertest.php 中的 unique_dates 在第 29 行警告:in_array() 期望参数 2 是数组,在C:\UniServer\UniServer\www\boaz9REDUX\xmlreadertest.php 在第 29 行
    • 确保您正确定义了数组:$unique_dates = array();
    • xpath('/rtnshowtime/filmtitle/show[preceding-sibling::shortname="AGOODDAYTODIEHARD"]'); foreach ($dateResults as $dateResult) { // 如果日期不存在于唯一日期数组中 if ( !in_array($dateResult->date, $unique_dates)) { // 添加到唯一日期数组 $unique_dates[] = $日期结果->日期; print_r($dateResult->date);回声“
      ”; } } ?>
    • 产生:SimpleXMLElement 对象 ( [0] => 02152013 ) SimpleXMLElement 对象 ( [0] => 02152013 ) SimpleXMLElement 对象 ( [0] => 02152013 ) SimpleXMLElement 对象 ( [0] => 02162013 ) SimpleXMLElement 对象 ( [0] => 02162013 )
    • 啊,我明白了,$dateResult-&gt;date 是一个对象。如果您将出现的$dateResult-&gt;date 替换为来自该对象的值,这将起作用。您想将数字放入数组并检查该值。
    【解决方案3】:

    好的,这就是我选择的。

    $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 语句中,它是一个对象而不是数组结果。我假设...我对术语不是很熟悉。相反,我选择了一个空格和 - 每次之前和 - 每次之后都有空格。它现在必须工作。 :)

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

    【讨论】:

    • 我未能添加,这只解决了我与此 xml 提要有关的问题之一。我将不得不编写一行代码,给出今天的日期,例如 02152013,并确保它存在于 xml 中。那么我将不得不将该日期增加一天...我可能会使用 strtotime() 来执行此操作,然后查看该日期是否存在于 xml 中。如果是,则将其作为链接回显,然后再次增加日期,依此类推,直到 xml 中不存在该日期并停止。
    猜你喜欢
    • 2016-03-01
    • 1970-01-01
    • 2014-10-26
    • 1970-01-01
    • 2020-05-06
    • 2021-09-24
    • 2018-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多