删除数组重复PHP [英] Remove array duplicates PHP
本文介绍了删除数组重复PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从数组中删除重复数据?
How do I remove duplicates from an array?
我有两个数组名为 $ array
和 $ new_array
。 $ array
有内容,而 $ new_array
为空,如下所示:
Let's say that my I have two arrays named $array
and $new_array
. $array
has contents while $new_array
is empty, as seen below:
$array = array(5,1,2,1,5,7,10);
$new_array = array();
我想要 $ new_array
存储唯一 $ array
的值。它是这样的:
I want $new_array
to store the unique values of $array
. It kind of goes like this:
$array = array(5,1,2,1,5,7,10);
$new_array = array(5,1,2,7,10); // removing the 1 and 5 after 2 since those numbers are already a duplicate of the preceding numbers.
echo $new_array; // Output: 512710
推荐答案
可以通过PHP array_unique 功能。
You can do it through PHP's array_unique function.
此函数遍历所提供的数组,并返回具有唯一值的数组(重复值将被删除)。
This function traverses through your provided array and returns an array with unique values (repeating values will be removed).
返回所需字符串的代码:
Code to return desired string:
$array = array(5,1,2,1,5,7,10);
$new_array = array_unique($array);
echo implode('', $new_array);
这篇关于删除数组重复PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文