gpt4 book ai didi

php - array_unique 只返回一条记录

转载 作者:行者123 更新时间:2023-12-02 07:05:53 25 4
gpt4 key购买 nike

在下面的代码中:

$sth = $dbh->query('SELECT DISTINCT title,courseId,location from training');  
$sth->setFetchMode(PDO::FETCH_ASSOC);
$results = $sth->fetchAll();
$uu = array_unique($results);

echo "<pre>";
print_r($uu);
echo "</pre>";

print_r($uu);我只得到 1 个结果

如果我删除 array_unique 将返回所有(30+)行。 (不,不是所有的都是重复的):)

我做错了什么?

编辑 var_dump() 结果:

array(23) {
[0]=>
array(3) {
["title"]=>
string(26) "String Here"
["courseId"]=>
string(1) "8"
["location"]=>
string(1) "1"
}
[1]=>
array(3) {
["title"]=>
string(26) "Another String Here"
["courseId"]=>
string(1) "8"
["location"]=>
string(1) "2"
}
[2]=>
array(3) {
["title"]=>
string(24) "Third String Here"
["courseId"]=>
string(1) "5"
["location"]=>
string(1) "2"
}

等...

最佳答案

根据我对 array_unique 的理解,它不会递归地遍历数组。它将看到 $uu === array() 的每个值,因此在保留键的同时删除除一个值之外的所有值。如果结果 key 始终为 0,我不会感到惊讶。

在手册中看到注释:

Note that array_unique() is not intended to work on multi dimensional arrays.

关于php - array_unique 只返回一条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12081006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com