使用 array_unique 删除重复项
我创建了一个远程工作的图像上传器,因此每当用户输入一堆链接时,我想防止添加重复的链接,这样图像就不会被复制两次并被删除,从而使链接保持唯一,没有任何链接重复。
$break = explode("\n", $links);
$count = count($break);
$unique_images = array();
for($i = 0; $i < $count; $i++)
{
array_push($unique_images, $break[$i]);
}
array_unique($unique_images);
其余的代码可以工作,但我只是不明白为什么它不起作用,我还尝试了 foreach 循环,但这也没有帮助。
我已将 error_reporting
设置为 E_ALL
但没有错误。我在数组上使用 var_dump
并得到以下信息:
array(3)
{
[0]=> string(48) "http://localhost:8888/images/img/wallpaper-1.jpg"
[1]=> string(48) "http://localhost:8888/images/img/wallpaper-1.jpg"
[2]=> string(48) "http://localhost:8888/images/img/wallpaper-1.jpg"
}
为什么 array_unique
不删除任何重复项?
I created a image uploader which works by remote, so whenever a user enters a bunch of links, I want to prevent duplicate links being added so that the image isn't copied twice and is removed so it leaves the links to be unique without any duplicates.
$break = explode("\n", $links);
$count = count($break);
$unique_images = array();
for($i = 0; $i < $count; $i++)
{
array_push($unique_images, $break[$i]);
}
array_unique($unique_images);
The rest of the code works, but I'm just not getting why it isn't working, I also tried a foreach
loop but that didn't help as well.
I have error_reporting
set to E_ALL
but there are no errors. I use var_dump
on the array and I get this:
array(3)
{
[0]=> string(48) "http://localhost:8888/images/img/wallpaper-1.jpg"
[1]=> string(48) "http://localhost:8888/images/img/wallpaper-1.jpg"
[2]=> string(48) "http://localhost:8888/images/img/wallpaper-1.jpg"
}
How come the array_unique
doesn't remove any duplicates?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
array_unique
返回过滤后的数组,而不是改变它。将最后一行更改为:它应该可以工作。
array_unique
returns the filtered array, instead of altering it. Change your last line into:and it should be working.
array_unique()
返回< /strong> 一个新数组,它不会就地修改数组:array_unique()
returns a new array, it does not modify the array in place:您可以这样做:
array_unique
函数返回一个删除了重复项的新数组。所以需要收集它的返回值。此外,
explode
返回一个数组,您可以将其直接提供给array_unique
。You can just do:
The
array_unique
function returns a new array with duplicates removed. So you need to collect its return value.Also
explode
returns you an array which you can directly feed toarray_unique
.否则你只是丢弃结果
otherwise you're simply discarding the result