PHP array implementation method, php array implementation method. PHP array deduplication is a fast implementation method. php array implementation method overview use the array_unique () function of PHP to allow you to pass an array and then remove duplicate values, returns an implementation method that has unique PHP array de-duplication faster. php array implementation method
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.
Articles you may be interested in:
- Analysis on de-duplication of two-dimensional arrays in PHP
- Php array deduplication function code
- Php 3D array deduplication (sample code)
- Php array deduplication instance and analysis
- Example of deduplication of php arrays
- Php bubble sorting, quick sorting, quick search, two-dimensional array deduplication instance sharing
- Php two-dimensional array merging and deduplication
The array_unique () function of PHP allows you to pass an array, remove duplicate values, and return a unique...