1.php In_array Method Description
PHP finds that array elements exist, typically using the In_array method.
BOOL In_array (mixed $needle, array $haystack [, bool $strict = FALSE])
parameter Description:
Needle
The value to be searched, if needle is a string, the comparison is case-sensitive.
Haystack
The array used to compare
Strict
If the value of the third parameter strict is TRUE then the In_array () function also checks whether the needle type is the same as in haystack
return value
Returns TRUEif needle is found, otherwise FALSE.
2.in_array Find element efficiency
The In_array efficiency is very low when the comparison array is haystack large
Example: Using In_array to compare an array with 100,000 elements 1000 times
<?php$arr = Array ();//Create an array of 100,000 elements for ($i =0; $i <100000; $i + +) { $arr [] = $i;} Record start Time $starttime = Getmicrotime ();//randomly create 1000 numbers using In_array compare for ($j =0; $j <1000; $j + +) { $str = Mt_rand (1,99999 ); In_array ($str, $arr);} Record end Time $endtime = Getmicrotime (); Echo ' run: '. (float) (($endtime-$starttime) *1000). ' Ms<br> ';/** * Get microtime * @return float */function getmicrotime () { list ($usec, $sec) = Explode (", Microtime ( )); return (float) $usec + (float) $sec;}? >
Run time:2003.6449432373ms
Use In_array to determine whether an element exists, compare 1000 times in an array of 100,000 elements, and run time takes about 2 seconds
3. Improve the method of finding element efficiency
We can use array_flip for key-value swaps, and then use the isset method to determine whether an element exists, which can improve efficiency.
Example: Using Array_flip first to Exchange key values, and then use the Isset method to determine the 100,000 elements in the array 1000 times
<?php$arr = Array ();//Create an array of 100,000 elements for ($i =0; $i <100000; $i + +) { $arr [] = $i;} Key value Interchange $arr = Array_flip ($arr);//record start time $starttime = Getmicrotime ();//randomly create 1000 numbers using Isset compare for ($j =0; $j <1000; $j + +) { $str = Mt_rand (1,99999); Isset ($arr [$str]);} Record end Time $endtime = Getmicrotime (); Echo ' run: '. (float) (($endtime-$starttime) *1000). ' Ms<br> ';/** * Get microtime * @return float */function getmicrotime () { list ($usec, $sec) = Explode (", Microtime ( )); return (float) $usec + (float) $sec;}? >
Run time:1.2781620025635ms
Use array_flip and isset to determine whether an element exists, compare 1000 times in an array of 100,000 elements, and run time takes about 1.2 milliseconds
Therefore, comparing large arrays, using the array_flip and isset methods is much more efficient than In_array .
This article explains the ways to improve efficiency by finding array elements in PHP, and more about the PHP Chinese web.
Related recommendations:
Explanation of MySQL strict mode Strict modes
PHP uses explode split string for beginners easy to ignore problems explained
Explanation of two-column data methods in MySQL interchange table