Php 对于每个循环,如果此结果与上一个相同,则跳到不相同的下一个

Php 对于每个循环,如果此结果与上一个相同,则跳到不相同的下一个,php,object,xpath,foreach,skip,Php,Object,Xpath,Foreach,Skip,我正在运行一个foreach循环以从xml文件获取数据。xml文件多次列出相同的日期,每次都有不同的数据。我需要做的是每个日期只显示一次 基本上,我需要foreach循环在第一个循环中显示第一个日期(对象)。如果第二、第三、第四个循环中的日期(对象)相同,则跳过该循环并移动到下一个日期(对象)不相同的循环。 以下是我现在拥有的: $dateResults = $xml->xpath('/rtnshowtime/filmtitle/show[preceding-sibling::shortn

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

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

$dateResults = $xml->xpath('/rtnshowtime/filmtitle/show[preceding-sibling::shortname="AGOODDAYTODIEHARD"]');
foreach ($dateResults as $dateResult) {
    print_r($dateResult->date);
    echo "<br>";
}
$dateResults=$xml->xpath('/rtnshowtime/filmtitle/show[previous sibling::shortname=“AGOODDAYTODIEHARD”]);
foreach($dateResults作为$dateResult){
打印($dateResult->date);
回声“
”; }
这将产生:

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
SimpleXMLElement对象([0]=>02152013)
SimpleXMLElement对象([0]=>02152013)02152013)02162013)
SimpleXMLElement对象([0]=>02162013)02162013)02162013)02162013)02172013)
SimpleXMLElement对象([0]=>0217213)0217213)0217213)0217213)02182013)
SimpleXMLElement对象([0]=>02182013)02192013)
SimpleXMLElement对象([0]=>02192013)02202013)
SimpleXMLElement对象([0]=>02202013)02212013)

SimpleXMLElement对象([0]=>02212013)您可以尝试将日期作为键并检查日期是否已被使用

$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>";
    }
}
//创建用于存储唯一日期的数组
$unique_dates=array();
$dateResults=$xml->xpath('/rtnshowtime/filmtitle/show[previous sibling::shortname=“AGOODDAYTODIEHARD”]);
foreach($dateResults作为$dateResult)
{
//需要此项才能从对象获取时间属性
$time_索引=0;
//如果日期不存在于唯一日期数组中
如果(!在数组中($dateResult->date->$time\u index,$unique\u dates))
{
//添加到唯一日期数组
$unique\u dates[]=$dateResult->date->$time\u index;
打印($dateResult->date);
回声“
”; } }
好的,这是我选择的

$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."- ";
        }
    }
}
我使用$\u GET从URL中获取日期和短名称,然后使用短名称决定我将处理xml中的哪部电影。然后我用第一个foreach生成结果。然后在第一个foreach中运行第二个foreach,专门处理包含日期和时间的子元素。然后,我使用一个if语句根据URL分隔我要处理的日期。由于if语句,我可以在结果中回显兄弟姐妹日期相同的所有时间。我想重复上一次的时间,比如:1300、1400、1500、1600,后面没有逗号,但我不知道怎么做。我尝试使用IMPODE(),但因为每次echo都在if语句中,所以它是一个对象,而不是数组结果。我想。。。我对这个术语不太熟悉。相反,我选择了一个空格和-每次之前和-每次之后和空格。它现在必须工作。:)


谢谢大家的帮助!堆垛溢出岩石

它在第28行的C:\UniServer\UniServer\www\boaz9REDUX\xmlreadertest.php中返回以下内容:注意:未定义变量:在第29行的C:\UniServer\UniServer\www\boaz9REDUX\xmlreadertest.php中返回日期警告:array_key_exists()期望参数2为数组,第29行C:\UniServer\UniServer\www\boaz9REDUX\xmlreadertest.php中的null警告:第30行C:\UniServer\UniServer\www\boaz9REDUX\xmlreadertest.php中的偏移量类型非法。我对php的了解非常原始。我无法解释循环、数组、对象等,但我知道它们是不同的。当我尝试使用array_unique时,它基本上说我没有输出数组。我修复了一个语法错误,该错误将
$date=$dateResult->$date
(未定义变量:date)改为
$date=$dateResult->date
。确保添加
$finalResults=array()在循环之前。该警告是因为
$finalResults
正在读取
NULL
当前警告:数组\u键\u存在():第33行的C:\UniServer\UniServer\www\boaz9REDUX\xmlreadertest.php中的第一个参数应为字符串或整数警告:第35行的C:\UniServer\UniServer\www\boaz9REDUX\xmlreadertest.php中的非法偏移量类型为:$finalResults[$date]=$dateResult;生成:注意:未定义变量:第29行C:\UniServer\UniServer\www\boaz9REDUX\xmlreadertest.php中的unique_日期警告:in_array()期望参数2为数组,第29行C:\UniServer\UniServer\www\boaz9REDUX\xmlreadertest.php中给出的null确保正确定义数组:
$unique_dates=array()xpath('/rtnshowtime/filmtitle/show[previous sibling::shortname=“AGOODDAYTODIEHARD”]);foreach($dateResults as$dateResult){//如果日期不存在于唯一日期数组中,如果(!在数组中($dateResult->date,$unique_dates)){//添加到唯一日期数组$unique_dates[]=$dateResult->date;打印($dateResult->date);回显“
”;}}>”生成:SimpleXMLElement对象([0]=>02152013)simplexmlement对象([0]=>02152013)simplexmlement对象([0]=>02152013)simplexmlement对象([0]=>02162013)simplexmlement对象([0]=>02162013)啊,我明白了,
$dateResult->date
是一个对象。如果将出现的
$dateResult->date
替换为该对象的值,则此操作有效。您希望将数值输入数组并检查该值。我添加失败,这只解决了我与此xml提要的一个问题。我必须编写一行代码,给出今天的日期,比如02152013,并确保它存在于xml中。那我就得把日期延长一天。
showtimes for 02152013
 -1300-  -1400-  -1500-  -1600-  -1700-