Home >Backend Development >PHP Problem >How to determine whether an element exists in a php array
PHP is a dynamic language that allows developers to dynamically create and modify data structures at runtime. Among them, one of the most commonly used data structures is arrays.
Array is a data type used to store a set of data. In PHP, arrays can be created and accessed in different ways. In actual projects, we usually need to judge the elements in the array to ensure the correctness of the program. This article will introduce how to determine whether an element in an array exists in PHP.
The in_array() function is a function in PHP used to check whether a value exists in an array. The syntax of this function is as follows:
bool in_array( mixed $needle, array $haystack [, bool $strict = FALSE ] )
Among them, $needle represents the value to be found, and $haystack represents Target array, $strict indicates whether strict mode is enabled.
The following is an example of using this function:
$fruits = array('apple', 'banana', 'orange'); if( in_array('apple', $fruits) ){ echo '苹果存在!'; }else{ echo '苹果不存在..'; }
In the above code, an array named $fruits is first defined, and then the in_array() function is used to determine whether there is a string in it. apple'. If it exists, output "Apple exists!", otherwise output "Apple does not exist..".
It should be noted that the in_array() function uses relaxed mode by default (that is, the third parameter is FALSE). If you want to turn on strict mode, you must set it to TRUE.
The array_key_exists() function is used to determine whether the specified key exists in the array. The syntax is as follows:
bool array_key_exists( mixed $key, array $array )
Among them, $key represents the key to be found, and $array represents the target array.
The following is an example of using this function:
$students = array('Tom' => 18, 'Jerry' => 20, 'Mickey' => 21); if( array_key_exists('Jerry', $students) ){ echo 'Jerry存在!'; }else{ echo 'Jerry不存在..'; }
In the above code, an array named $students is first defined, which stores some key-value pairs. Then use the array_key_exists() function to check if it contains an element with the key name 'Jerry'. If it exists, output "Jerry exists!", otherwise output "Jerry does not exist..".
It should be noted that the array_key_exists() function can only be used to determine whether the specified key name exists in the array, but cannot determine whether the key value exists.
isset() function is used to determine whether the variable has been defined and is not NULL. In an array, we can use the isset() function to check whether a specified element exists. The syntax is as follows:
bool isset( mixed $var [, mixed $... ] )
Among them, $var can be a variable name or an array element. If you are detecting array elements, you need to use the following format:
isset( $array['key'] );
The following is an example of using the isset() function:
$fruits = array('apple', 'banana', 'orange'); if( isset($fruits[0]) ){ echo '下标为0的元素存在!'; }else{ echo '下标为0的元素不存在..'; }
In the above code, use the isset() function to detect the array Whether there is an element with index 0 in $fruits. If it exists, output "The element with subscript 0 exists!", otherwise output "The element with subscript 0 does not exist..".
It should be noted that the isset() function can only be used to determine whether a variable or array element has been defined and is not NULL. If you need to determine whether the value of an array element is empty, you should use the empty() function.
The array_search() function is used to search for the specified value in the array and return its corresponding key name. The syntax is as follows:
mixed array_search( mixed $needle, array $haystack[, bool $strict = FALSE ] )
Among them, $needle represents the value to be found, and $haystack represents the target array. , $strict indicates whether to enable strict mode.
The following is an example of using this function:
$fruits = array('apple', 'banana', 'orange'); $key = array_search('apple', $fruits); if($key !== FALSE){ echo '苹果对应的键名是:'.$key; }else{ echo '苹果不存在..'; }
In the above code, an array named $fruits is first defined, and then the array_search() function is used to check whether it contains a string. 'apple'. If it exists, return the key name corresponding to the element in the array; otherwise, return FALSE.
It should be noted that the array_search() function can only find values and cannot determine whether the key exists.
Summary
This article introduces four commonly used array element judgment methods in PHP: in_array(), array_key_exists(), isset() and array_search(). Different methods have their own advantages and disadvantages, and developers should choose according to the actual situation. At the same time, when using these functions, you also need to pay special attention to the type of parameters and whether strict mode is turned on.
The above is the detailed content of How to determine whether an element exists in a php array. For more information, please follow other related articles on the PHP Chinese website!