array_unique 在 PHP 中不起作用?

发布于 2024-12-21 09:33:38 字数 794 浏览 0 评论 0原文

我的 MySql 数据库中有一个表:

  pageid   | text
-----------+----------
    1      | test
-----------+----------
    2      | example
-----------+----------
    3      | another
-----------+----------
    1      | example1

和这个 PHP 代码:

$query = "SELECT pageid FROM test";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
  {
  $id = $row['pageid'];
  $id1 = array($id);
  $id2 = array_unique($id1);
  foreach ($id2 as $value)
    {
    echo $value . "<br>";
    }
  }

但它返回:

1
2
3
1

我想让 pageid 唯一。所以我想回显一次数字1。就像这样:

1
2
3

那么我做错了什么?我怎样才能实现我想要的目标?

预先感谢...

[我知道这段代码没有多大意义,但这只是这个问题的演示,我有一个更复杂的代码:)]

I have got a table in my MySql database:

  pageid   | text
-----------+----------
    1      | test
-----------+----------
    2      | example
-----------+----------
    3      | another
-----------+----------
    1      | example1

and this PHP code:

$query = "SELECT pageid FROM test";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
  {
  $id = $row['pageid'];
  $id1 = array($id);
  $id2 = array_unique($id1);
  foreach ($id2 as $value)
    {
    echo $value . "<br>";
    }
  }

But it returns:

1
2
3
1

I would like to make the pageids unique. So I would like to echo out the number 1, once. Like this:

1
2
3

So what am I doing wrong? How could I achieve what I want?

Thanks in advance...

[I know that this code doesn't make much sense, but this is just a demo for this question and I have a much more complicated code :) ]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

疯到世界奔溃 2024-12-28 09:33:38

当然,在这种情况下真正的答案是首先选择不同的记录:

$query = "SELECT DISTINCT pageid FROM test";

但是至于为什么你的代码不起作用......

你将 $id1 声明为数组,但不附加到它。相反,您每次只是创建一个 1 元素数组。相反,附加所有结果,然后调用 array_unique()

$query = "SELECT pageid FROM test";
$result = mysql_query($query);

// Declare array first
$id1 = array();
while($row = mysql_fetch_array($result))
  {
  $id = $row['pageid'];
  // Append to the array...
  $id1[] = $id;
  }

// Call array_unique() outside the loop:
$id2 = array_unique($id1);
foreach ($id2 as $value)
{
  echo $value . "<br>";
}

Of course, the real answer in this situation is to select distinct records to begin with:

$query = "SELECT DISTINCT pageid FROM test";

But as to why your code doesn't work...

You are declaring $id1 as an array, but not appending to it. Instead you're just creating a 1 element array each time. Instead, append all results then call array_unique()

$query = "SELECT pageid FROM test";
$result = mysql_query($query);

// Declare array first
$id1 = array();
while($row = mysql_fetch_array($result))
  {
  $id = $row['pageid'];
  // Append to the array...
  $id1[] = $id;
  }

// Call array_unique() outside the loop:
$id2 = array_unique($id1);
foreach ($id2 as $value)
{
  echo $value . "<br>";
}
落日海湾 2024-12-28 09:33:38

您可以在 SELECT 中执行以下操作:SELECT DISTINCT pageid FROM test 或使用 GROUP BY,如下所示:

SELECT pageid FROM test WHERE 1 GROUP BY pageid 

You could do it in your SELECT by doing something like SELECT DISTINCT pageid FROM test or by using GROUP BY like this:

SELECT pageid FROM test WHERE 1 GROUP BY pageid 
锦上情书 2024-12-28 09:33:38

您可以在 sql 查询中使用 Distinct 关键字。这将返回 Page 的唯一值

You can use Distinct keyword in your sql query.That will return you the unique vaues of Page

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文