Analysis of faster implementation of PHP array de duplication

  • 2021-10-11 17:44:47
  • OfStack

In this paper, an example shows a faster implementation of PHP array deduplication. Share it for your reference, as follows:

Overview

Using PHP array_unique() The function allows you to pass an array, then remove duplicate values and return an array with only one value. This function works well in most cases. However, if you try to use it in a large array, array_unique() Function, it will run 1% slower.

There is a better and faster function array_flip() Instead of using array_unique() Function to create an array of only 1. This magical function swaps the key and value of every 1 element in the array, because the key value must be only 1, so you will get the following array_unique() The result of function 1.

PHP code:


/*  Create 1 Object that contains duplicate values, 1 Altogether 4 An array of three elements  */
$array = array('green','blue','orange','blue');
/*  Flip the array, and you will get only 1 Array of key values 
    array('green'=>0,'blue'=>1,'orange'=>2); */
$array = array_flip($array);
/*  And then flip it over 1 Second, replace the keys and values, and then get the array: array(0=>'green',1=>'blue',2=>'orange'); */
$array = array_flip($array);
print_r($array)

Run results:


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

Because we have removed 1 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 want the key values of an array to keep a sequence of numbers, you can use one or two methods to solve the problem of out-of-order key values.

Repair keys of Array with array_merge

Add array_flip The following function will sort the key values of the array and restore them to the normal sequence, such as: 0, 1, 2, 3 …

PHP code:


$array = array('green','blue','orange','blue');
$array = array_flip($array);
$array = array_flip($array);
/*  Use array_merge() Function to repair key value */
$array = array_merge($array);
print_r($array)

The running results are the same as above

In the second way, use array_keys

Note that this method of repairing array key values is better than using array_merge() Function is slightly faster by 1 point. You can also use it in combination with the last step 1 array_keys() Function (this function returns the flipped value). Then when you flip the values of the array, the key values will be created in order.

PHP code:


$array = array('green','blue','orange','blue');
$array = array_flip($array);
/*  Follow the first 1 Examples 1 Sample, but now let's extract the key value of the array first  */
$array = array_keys($array);
print_r($array)

The running results are the same as above

Conclusion

Very simple, compared with using a large array array_unique Function, there is an effective performance improvement.

PS: This site also has two relatively simple and practical online text to repeat tools, recommended for everyone to use:

Online Duplicate Removal Tool:
http://tools.ofstack.com/code/quchong

Online text de-duplication tool:
http://tools.ofstack.com/aideddesign/txt_quchong

For more readers interested in PHP related contents, please check the topics of this site: "PHP Array (Array) Operation Skills Complete Book", "PHP Common Traversal Algorithms and Skills Summary", "php String (string) Usage Summary", "php Common Functions and Skills Summary", "PHP Error and Exception Handling Methods Summary", "PHP Basic Grammar Introduction Course", "php Object-Oriented Programming Introduction Course" and "PHP Mathematical Operation Skills Summary"

I hope this article is helpful to everyone's PHP programming.


Related articles: