简体   繁体   English

PHP多次foreach相同的值,但只打印一次

[英]php foreach same value multiple times but only print one time

I have a xml file with Shows and Seasons in it. 我有一个带有Shows and Seasons的xml文件。

What i'm trying to do is to read out the seasons for one show. 我想做的是读出一场演出的季节。 But the problem is that each seasons is represented multiple times under the same show. 但是问题在于,每个季节在同一场演出中被多次代表。

I only want each season number printed out one time like: 我只希望将每个季节编号打印一次,例如:

Season 1
Season 2

But what i got now is: 但是我现在得到的是:

Season 1
Season 2
Season 2
Season 1
Season 1

My xml looks like 我的xml看起来像

<?xml version="1.0"?>
<episodeview>
<episodeview>
    <idShow>1</idShow>
    <idSeason>1</idSeason>
</episodeview>
<episodeview>
    <idShow>1</idShow>
    <idSeason>2</idSeason>
</episodeview>
<episodeview>
    <idShow>1</idShow>
    <idSeason>2</idSeason>
</episodeview>
<episodeview>
    <idShow>1</idShow>
    <idSeason>1</idSeason>
</episodeview>
<episodeview>
    <idShow>1</idShow>
    <idSeason>1</idSeason>
</episodeview>
</episodeview>

And my php file: 和我的PHP文件:

<?php
$idShow = "1";
$source = "show.xml";

$xmlstr = file_get_contents($source);
$xmlcont = new SimpleXMLElement($xmlstr);
foreach($xmlcont as $url) {
    if ($url->idShow == $idShow) {
        $test = $url->idSeason;
        echo "Season ";
        echo $test;
        echo "<br>";
     }      
}

?> ?>

try this, short and sweet: 尝试一下,简短而有趣:

 $xml=simplexml_load_file($source); // (1)

 foreach ($xml->xpath("//idSeason") As $season) { // (2)

      $s[(int)$season]=$season; // (3)
 }

 foreach ($s As $a) echo "Season $a<br />"; // (4)
  1. get a simplexml -Element from the file 从文件中获取一个simplexml -Element

  2. select the <idSeason> -nodes only using xpath and iterate through them 仅使用xpath选择<idSeason> -nodes并遍历它们

  3. create a new array $s with the season-id as index and as value --> an array-index has to be unique, so duplicate ids won't enlarge the array, but "overwrite" the index if it already existed. 创建一个新的数组$s ,其季节ID为索引和值->数组索引必须是唯一的,因此重复的ID不会扩大数组,但是如果索引已经存在,则“覆盖”该索引。

  4. iterate through $s to echo it 遍历$s以回显它

I am aware that one could select only unique values by xpath , but I'm not that skilled ;-) and simplexml is not supporting xpath 2.0 which is easier. 我知道一个人只能通过xpath选择唯一值,但是我不是那么熟练;-)并且simplexml不支持xpath 2.0,这更容易。

Try: 尝试:

<?php
$idShow = "1";
$source = "show.xml";
$have = array();

$xmlstr = file_get_contents($source);
$xmlcont = new SimpleXMLElement($xmlstr);
foreach($xmlcont as $url) {
    if ($url->idShow == $idShow) {
        $test = $url->idSeason;
         if( ! in_array( $test, $have) ){ //Check if the season already is displayed
           echo "Season ";
           echo $test;
           echo "<br>";
           $have[] = $test; //Store the season in the array
          }
     }      
}

This way you store everything you displayed in the array, and before you output test, it checkes if it is displayed already. 这样,您将存储在数组中显示的所有内容,并在输出测试之前检查它是否已显示。

I'd go about it like this: 我会这样处理:

<?php
  $idShow = "1";
  $source = "show.xml";

  $xmlstr  = file_get_contents($source);
  $xmlcont = new SimpleXMLElement($xmlstr);
  $seasons = array();
  foreach($xml_cont as $url){ $seasons[] = $url->idSeason; }

  $seasons = array_uniq($seasons);

  foreach($seasons as $season){ echo "Season $season <br />"; } 
?>

Granted, my example involves more looping than some other solutions you might try, but I'd argue that it's fairly efficient, and perhaps just as importantly, readable. 诚然,我的示例比您可能尝试的其他解决方案涉及更多的循环,但我认为它相当有效,并且可能同样重要,而且可读性强。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM