0

我有一个输出到文本文件的日历脚本。我正在打开文本文件,将其读入数组,然后输出结果。文本文件包含:

7/9/2013-7/13/2013
Hot Stuff
By Robert More. Yes, folks, it's all platform shoes, leisure suits..
hotstuff.jpg
1,1,0,
*-*
7/16/2013-7/20/2013
Hot Stuff
By Robert More. Yes, folks, it's all platform shoes, leisure suits.. 
hotstuff.jpg
1,1,0,
*-*

我的 PHP 代码如下所示:

$content = file('DC_PictureCalendar/admin/database/cal2data.txt');
$content_chunked = array_chunk($content, 6);
            if (count($content_chunked > 0))
            {
                echo "<table>";
                for ($i=0;$i<count($content_chunked);$i++)
                {
                    echo "<tr>";
                    echo "<td valign='top'>";
                    echo "<div style='padding-top:6px;'>";
                    echo "<a href='schedule.php'>";
                    echo "<img src='DC_PictureCalendar/admin/database/images/".$content_chunked[$i][3]."' width='80' height='80' border='2'>";
                    echo "</a>";
                    echo "</div>";
                    echo "</td>";
                    echo "<td valign='top'>";
                    echo "<div style='padding-left:5px;'>";
                    echo "<table>";
                    echo "<tr>";
                    echo "<td>";
                    echo "<h2>";
                    echo "<a href='schedule.php'>";
                    echo $content_chunked[$i][1];
                    echo "</a>";
                    echo "</h2>";
                    echo "</td>";
                    echo "</tr>";
                    echo "<tr>";
                    echo "<td>";
                    echo $content_chunked[$i][2];
                    echo "<a class='green' href='schedule.php'>";
                    echo "Read more..";
                    echo "</a>";
                    echo "</td>";
                    echo "</tr>";
                    echo "</table>";
                    echo "</div>";
                    echo "</td>";
                    echo "</tr>";
                }
                echo "</table>";
            }

问题是,如果 $content_chunked[$i][1] (在这种情况下是标题)中有重复的条目,我只想显示一次而不是两次。这可能吗?我认为 array_unique 可能有效,但似乎没有帮助。提前致谢!

4

2 回答 2

0

array_unique() 返回没有重复的数组,但不修改原始数组!

所以:

$a = [1, 1, 2, 3]
array_unique($a) => [1, 2, 3]
$a => [1, 1, 2, 3]

新数组需要保存在一个变量中,以便您以后访问它。

$a = [1, 1, 2, 3]
$b = array_unique($a)
$b => [1, 2, 3]
于 2013-02-13T00:26:55.077 回答
0

尽管它可能不是最优雅/最有效的..在相关位置添加:

echo "<table>";
$titles = array();
for ($i=0;$i<count($content_chunked);$i++)
{
      if (in_array($content_chunked[$i][1], $titles)) continue;
      $titles[] = $content_chunked[$i][1]
于 2013-02-13T00:28:06.753 回答