How to find whether an element exists in a php array? You can consider using array_key_exists, in_array, array_search and other functions. This article will give you a brief introduction and interested friends can use it as a reference.
Contents of this section: Find an element in an array In PHP programming, there are three ways to find whether an element is in an array: in_array 'Function searches an array for a given value. in_array(value,array,type)type optional. If this parameter is set to true, it is checked whether the type of the searched data and the value of the array are the same. array_key_exists 'array_key_exists() function Determine whether the specified key exists in an array. If the key exists, return true, otherwise return false. array_search 'array_search() function Like in_array(), finds a key value in an array. If the value is found, the key of the matching element is returned. If not found, returns false. Instructions: When the amount of data is not large, such as less than 1000, any search method can be used and it will not become a bottleneck; When the amount of data is relatively large, array_key_exists is more appropriate. Of course, the memory occupied by array_key_exists here is relatively large. After calculation, the array structure is: array(1, 2, 3, ..) and array(1 => true, 2 => false, ..)Memory usage ratio is 1:2; This has something to do with the internal implementation. In fact, the first and second data structures in PHP are similar, both are associative arrays. |