PHP array deduplication method

Source: Internet
Author: User
The array_unique () function of PHP allows you to pass an array, remove duplicate values, and return an array with unique values, this article describes how to implement PHP array deduplication quickly. For more information, see this article. Overview

The array_unique () function of PHP allows you to pass an array, remove duplicate values, and return an array with unique values. This function works well in most cases. However, if you try to use the array_unique () function in a large array, it will run slowly.

There is a better and faster function array_flip () to replace the array_unique () function to create a unique array. This magic function exchanges the keys and values of each element in the array. because the key value must be unique, you will get the same result as the array_unique () function.

Faster PHP array deduplication

/* Create an array containing repeated values and a total of four elements */$ array = array ('green', 'Blue ', 'Orange', 'Blue '); /* flip the array and you will get the array ('green' => 0, 'Blue '=> 1, 'Orange' => 2) with the unique key value ); */$ array = array_flip ($ array);/* flip it again, place the key and value again, and obtain the array: array (0 => 'green ', 1 => 'blue', 2 => 'Orange '); */$ array = array_flip ($ array );

Because we have removed some elements, the array does not look like a normal sequence. For example, we may obtain array (0 => 'A', 2 => 'B', 5 => 'C ');. In some cases, this is not a problem, but if you need the array key value to keep the sequence of numbers, you can use one or two methods to solve the key value disorder.

Use array_merge to fix the array keys

The function after array_flip is added will sort the key values of the array and restore them to a normal sequence, for example, 0, 1, 2, 3...

$ Array = array ('green', 'Blue ', 'Orange', 'Blue '); $ array = array_flip ($ array ); /* use the array_merge () function to fix the key value */$ array = array_merge ($ array );
 

Method 2: Use array_keys

Note that this method of repairing the array key value is a little faster than using the array_merge () function. You can also use the array_keys () function in the last step (this function returns the value after the flip ). Then, when you flip the value of the array, the key value will be created in order.

$ Array = array ('green', 'Blue ', 'Orange', 'Blue '); $ array = array_flip ($ array);/* Same as the first example, but now we extract the key value of the array */$ array = array_keys ($ array );

Conclusion

It is very simple. compared to using the array_unique function in a large array, it has an effective performance improvement.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.