1. Use the Array_unique method to remove the weight
To de-weigh the elements of an array, we generally use the Array_unique method, which can be used to weigh the elements in the arrays.
<?php$arr = Array (1,1,2,3,3,3,4,4,5,6,6,7,8,8,9,9,9), $arr = Array_unique ($arr), $arr = Array_values ($arr);p Rint_r ( $arr);? >
Output:
Array ( [0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = 5 [5] = 6 [6] = = 7
[7] + 8 [8] = 9)
After the weight is gone, the key values are not in order, and the key values can be reordered by using Array_values.
2. Using the Array_unique method to remove weight efficiency
<?php$arr = Array ();//Create an array of 100,000 random elements for ($i =0; $i <100000; $i + +) { $arr [] = Mt_rand (1,99);} Record start Time $starttime = Getmicrotime ();//$arr = Array_unique ($arr);//record end time $endtime = Getmicrotime (); $arr = Array_value S ($arr); Echo ' unique count: '. Count ($arr). ' <br> '; Echo ' run Time: '. (float) (($endtime-$starttime) *1000). ' Ms<br> '; Echo ' use Memory: '. Getusememory ();/** * Get memory using * @return float */function getusememory () { $use _memory = Round (Memory_get_usage (True)/1024,2). ' KB '; return $use _memory;} /** * Get microtime * @return float */function getmicrotime () { list ($usec, $sec) = Explode (', microtime ()); return (float) $usec + (float) $sec;}? >
Unique count:99
Run TIME:653.39303016663MS
Use MEMORY:5120KB
Use Array_unique method to go heavy, run time takes about 650ms, memory occupies about 5m
3. Faster array de-weight method
PHP has a key-value interchange method Array_flip, we can use this method to go heavy, because the key value interchange, the original duplicate value will become the same key.
Then do a key exchange, the key and the value back can be done to the weight.
<?php$arr = Array ();//Create an array of 100,000 random elements for ($i =0; $i <100000; $i + +) { $arr [] = Mt_rand (1,99);} Record start Time $starttime = Getmicrotime ();//Use key value interchange to $arr = Array_flip ($arr); $arr = Array_flip ($arr);//record end time $endtime = Getmi Crotime (); $arr = Array_values ($arr); Echo ' unique count: '. Count ($arr). ' <br> '; Echo ' run Time: '. (float) (($endtime-$starttime) *1000). ' Ms<br> '; Echo ' use Memory: '. Getusememory ();/** * Get memory using * @return float */function getusememory () { $use _memory = Round (Memory_get_usage (True)/1024,2). ' KB '; return $use _memory;} /** * Get microtime * @return float */function getmicrotime () { list ($usec, $sec) = Explode (', microtime ()); return (float) $usec + (float) $sec;}? >
Unique count:99
Run time:12.840032577515ms
Use memory:768kb
Use Array_flip method to go heavy, run time takes about 18ms, memory occupies about 2m
Therefore, using the array_flip method to reduce the time-to-weight ratio using array_unique method reduces the 98%and memory consumption 4/5;
This article explains how to implement the PHP array elements to quickly redo the effect, more relevant content please focus on PHP Chinese web.
Related recommendations:
How to find array elements in PHP to improve efficiency
Explanation of MySQL strict mode Strict modes
PHP uses explode split string for beginners easy to ignore problems explained