简体   繁体   English

PHP 删除大数组中的重复项

[英]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);

Then i got 2 main errors:然后我有两个主要错误:

  • 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:我尝试过的:

  • i tried to search on google on who to remove duplicates in large arrays but didnt find the result i'm looking for我试图在 google 上搜索谁来删除大数组中的重复项,但没有找到我正在寻找的结果
  • and i have tried the function end($array) then array_push function to add the last element to my function, but it didnt work too, and i guess it isnt even the right solution.我已经尝试了函数 end($array) 然后使用 array_push 函数将最后一个元素添加到我的函数中,但它也不起作用,我想它甚至不是正确的解决方案。 and i didnt receive any clear error to share with you.我没有收到任何与您分享的明显错误。

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)
}

https://3v4l.org/DkETK https://3v4l.org/DkETK

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM