Home >Backend Development >PHP Tutorial >Detailed explanation of how to determine whether all arrays are the same in PHP
Use array_count_values() in php to complete
Because the first one of the constructed array is 1, and the next 99999 are all 0, construct an array again with the first 99999 being 1, and the last one being 0, as a comparison of the most and least times of a loop, and then Look at the running time of both: At this time, //Time spent using the loop method: Processed in 0.211106 second(s). //Time spent using the array_count_values method: Processed in 0.135076 second(s). When only one judgment is required, the time taken by the two methods is almost the same, but array_count_values is still faster. But when we perform the most comparisons and put the different array elements at the end of the array, the latter takes almost half the time of the former. array_count_values() What is the function: PHP has a built-in array_count_values() method in C language. This method is used to determine the number of occurrences of each array element. For example, if the array elements are 1, 1, 2, then it returns: Print the result: array(2) { [1]=> int(2) [2]=> int(1) } 1 appears twice and 2 appears once. At this time, determine how many elements there are in the array. If it is not 1, then there must be the same one. Use more built-in methods. Regardless of the language, the writing methods of experts are generally more efficient. Of course, there may be exceptions. When writing programs in PHP, I still feel that built-in functions are more efficient. |