Php 如何在for循环中只回显重复行一次?
我已将其编码为:Php 如何在for循环中只回显重复行一次?,php,Php,我已将其编码为: <?php $onlinecheck = mysql_query("SELECT * FROM online order by id DESC"); for($j=1; $onlinecheck1 = mysql_fetch_object($onlinecheck); $j++) { $page = $onlinecheck1->page; echo $page; } ?> 它将按如下方式打印:
<?php
$onlinecheck = mysql_query("SELECT * FROM online order by id DESC");
for($j=1; $onlinecheck1 = mysql_fetch_object($onlinecheck); $j++)
{
$page = $onlinecheck1->page;
echo $page;
}
?>
它将按如下方式打印:
index.php
index.php
contact.php
gallery.php
index.php
contact.php
gallery.php
但我想这样打印:
index.php
index.php
contact.php
gallery.php
index.php
contact.php
gallery.php
请帮助我制作一个关联数组,其键是您打印的名称。在打印名称之前,请检查它是否已在数组中
$pages_printed = array();
for($j=1; $onlinecheck1 = mysql_fetch_object($onlinecheck); $j++)
{
$page = $onlinecheck1->page;
if (!isset($pages_printed[$page])) {
$pages_printed[$page] = true;
echo $page;
}
}
//I have added missed > )
是否确实需要检查$used[$page]!==是否正确
?如果设置了,这是唯一可能的值。感谢您的努力@user3425372