Home  >  Article  >  Backend Development  >  How to remove duplicates from php array

How to remove duplicates from php array

angryTom
angryTomOriginal
2019-08-22 14:56:468939browse

How to remove duplicates from php array

The examples in this article describe the faster implementation of PHP array deduplication. Share it with everyone for your reference, the details are as follows:

Recommended tutorial: PHP video tutorial

Using PHParray_unique()The function allows you to pass an array and then remove duplicate values ​​and return an array with unique values. This function works well most of the time. However, if you try to use the array_unique() function on a large array, it will run slower.

Recommended manual:php complete self-study manual

There is a better and faster functionarray_flip()Instead of using the array_unique() function to create a unique array. This magical function will swap the key and value of each element in the array, because the key value must be unique, therefore, you will get the same result as the array_unique() function.

/* 创建一个包含重复值的,一共四个元素的数组 */
$array = array('green','blue','orange','blue');
/* 翻转数组,你将会得到唯一键值的数组
    array('green'=>0,'blue'=>1,'orange'=>2); */
$array = array_flip($array);
/* 然后再翻转一次,将键和值重新放置,然后得到数组:array(0=>'green',1=>'blue',2=>'orange'); */
$array = array_flip($array);
print_r($array)

Run result:

Array
(
  [0] => green
  [3] => blue
  [2] => orange
)

Because we have removed some elements, the array does not look like a normal sequence. For example, we may get: array(0=>'A',2=>'B',5=>'C');. In some cases, this is not a problem, but if you need the array keys to remain in numerical order, you can use one or two methods to solve the problem of key values ​​being out of order.

Use array_merge to repair the keys of the array

The function after adding array_flip will sort the keys of the array and let them Restore to the normal sequence, such as: 0,1,2,3...

$array = array('green','blue','orange','blue');
$array = array_flip($array);
$array = array_flip($array);
/* 使用array_merge()函数修复键值*/
$array = array_merge($array);
print_r($array)

The operation result is the same as above

The second way is to use array_keys

Note that this method of repairing array key values ​​is slightly faster than using the array_merge() function. You can also use the array_keys() function in the last step (this function returns the flipped value). Then when you flip the values ​​in the array, the keys are created in order.

$array = array('green','blue','orange','blue');
$array = array_flip($array);
/* 跟第一个例子一样,但是现在我们先提取数组的键值 */
$array = array_keys($array);
print_r($array)

The running results are the same as above

Conclusion

It is very simple. Compared with using the array_unique function in a large array, there is a Effective performance improvement.

Recommended related articles:
1.How to quickly implement array deduplication in PHP
2.PHP uses array_unique to deduplicate two-dimensional arrays
3.php Highly efficient deduplication of one-dimensional arrays
Related video recommendations:
1.Dugu Jiujian (4)_PHP video Tutorial

The above is the detailed content of How to remove duplicates from php array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn