从Chunked Array返回唯一值

时间:2013-02-13 00:02:59

标签: php string array-unique

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

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可能会起作用,但似乎没有帮助。提前谢谢!

2 个答案:

答案 0 :(得分: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]

答案 1 :(得分: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]