[英]PHP remove duplicates in a large array
I was removing duplicates from an array, i have used array_unique($array)
,我正在从数组中删除重复项,我使用了array_unique($array)
,
here an example for the array i tested on: [7 6 4 3 3 4 9]
.这是我测试的数组的示例: [7 6 4 3 3 4 9]
。
'the initial order of the values must be kept'
Here's my Function:这是我的功能:
function rmrepeat ($array) {
$array2 = array_unique($array);
return $array2;
}
// Input Array
$a=array(7, 6, 4, 3, 3, 4, 9);
$newArray = rmrepeat($a);
when i tested it on an array with 299 elements it didnt work.当我在一个包含 299 个元素的数组上测试它时,它不起作用。
second problem is that it didnt return the last element of the array第二个问题是它没有返回数组的最后一个元素
What i have tried:我尝试过的:
what can the problem be?问题是什么?
thanks in advance.提前致谢。
try this
$a=array(7,6,4,3,3,4,9);
$newArray = array_unique($a);
print_r($newArray);
or this one或者这个
$a=array(7,6,4,3,3,4,9);
$count = count($a);
$c = [];
for($i=0;$i<count($a);$i++){
if(!in_array($a[$i],$c)){
array_push($c,$a[$i]);
}
}
print_r($c);
Your code works fine.你的代码工作正常。 So I'm 100% sure that you wrongly thought that the last value is omitted.所以我 100% 肯定你错误地认为最后一个值被省略了。 In fact it is removed because it's repeated and you asked all duplicate values to be removed by running array_unique()
.事实上,它被删除是因为它是重复的,并且您要求通过运行array_unique()
删除所有重复值。 For a simple test run this code and check if you see those problems again.对于简单的测试,运行此代码并检查您是否再次看到这些问题。
function rmrepeat ($array) {
$array2 = array_unique($array);
return $array2;
}
$a = [];
for($i=0;$i<1000;$i++){
array_push($a, $i);
}
echo "Before removing duplicates: ", (string)count($a), PHP_EOL;
$a = rmrepeat($a);
echo "After removing duplicates: ", (string)count($a), PHP_EOL;
here there would not be any duplicated value so there will not be any change in the array length.这里不会有任何重复的值,所以数组长度不会有任何变化。 if you change array_push($a, $i);
如果你改变array_push($a, $i);
with array_push($a, rand(100, 200));
使用array_push($a, rand(100, 200));
then many indices would be deleted and in most cases last item will be deleted too.那么许多索引将被删除,并且在大多数情况下最后一项也将被删除。
In order to keep the last item when using array_unique you need to reverse the array first to make the last item first.为了在使用 array_unique 时保留最后一项,您需要先反转数组以首先制作最后一项。
$arr = ["a" => 7, "B"=>6, "C"=>4, "D"=>3, "E"=>3, "F"=>4, "G"=>9];
var_dump($arr);
$arr = array_reverse($arr); // reverse to remove first occurence
$arr = array_reverse(array_unique($arr)); // array_unique and reverse back again
var_dump($arr);
Now it will keep "E" => 3 and remove "D", and "F" remains but "C" is removed.现在它将保留“E”=> 3 并删除“D”,而“F”保留但“C”被删除。
https://3v4l.org/7P4IP https://3v4l.org/7P4IP
If order means keys then you can add true
as second parameter to array_revers to keep the keys.如果 order 意味着键,那么您可以将true
作为第二个参数添加到 array_revers 以保留键。
$arr = [1, 2, 5, 2, 9];
var_dump($arr);
$arr = array_reverse($arr, true);
$arr = array_reverse(array_unique($arr), true);
var_dump($arr);
returns回报
array(4) {
[0] => int(1)
// Notice key 1 is missing with value 2
[2] => int(5)
[3] => int(2)
[4] => int(9)
}
Another solution that could help, which I don't recommend is to override the default memory capacity.另一个可以提供帮助的解决方案是覆盖默认内存容量,但我不建议这样做。 You can do this using this:你可以使用这个来做到这一点:
ini_set('memory_limit', '-1')
Further Reading: http://php.net/memory_limit进一步阅读: http : //php.net/memory_limit
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.