Home >Backend Development >PHP Problem >PHP determines whether there is a null value in an array
In PHP programming, array processing is a very common operation. When we process an array, we may need to determine whether there is a null value in the array. This article will introduce how to use PHP to determine whether an array contains null values.
1. Use the in_array() function
The in_array() function is a very useful built-in function in PHP. This function is used to find a value in an array. Returns TRUE if the value exists in the array, otherwise returns FALSE. When determining whether there is a null value in an array, we can loop through the array to determine whether each element in the array is a null value. If a null value exists, TRUE is returned. The following is a code example that uses the in_array() function to determine whether there is a null value in the array:
function hasEmptyValue($arr){ foreach($arr as $value){ if(in_array($value, array(NULL, ''), true)){ return true; } } return false; } $arr1 = array('Tom', 'Jerry', 'Kate'); $arr2 = array('Tom', '', 'Kate'); if(hasEmptyValue($arr1)){ echo '数组$arr1存在空值'; }else{ echo '数组$arr1不存在空值'; } if(hasEmptyValue($arr2)){ echo '数组$arr2存在空值'; }else{ echo '数组$arr2不存在空值'; }
The output result is:
There is no null value in array $arr1
There is a null value in array$arr2
2. Use the empty() function
The empty() function is another convenient built-in function in PHP. This function is used to determine whether a variable is empty. When determining whether there is a null value in the array, we can iterate through each element in the array and use the empty() function to determine whether it is empty. Returns TRUE if there is an empty element. The following is a code example that uses the empty() function to determine whether there is a null value in the array:
function hasEmptyValue($arr){ foreach($arr as $value){ if(empty($value)){ return true; } } return false; } $arr1 = array('Tom', 'Jerry', 'Kate'); $arr2 = array('Tom', '', 'Kate'); if(hasEmptyValue($arr1)){ echo '数组$arr1存在空值'; }else{ echo '数组$arr1不存在空值'; } if(hasEmptyValue($arr2)){ echo '数组$arr2存在空值'; }else{ echo '数组$arr2不存在空值'; }
The output result is:
There is no null value in array $arr1
There is a null value in array$arr2
3. Use the foreach() function
In addition to using the built-in function, we can also use the foreach() function to determine whether there is a null value in the array. When traversing the array, we can use if to determine whether the array element is empty. If there is an empty element, it will return TRUE. The following is a code example that uses the foreach() function to determine whether there is a null value in the array:
function hasEmptyValue($arr){ foreach($arr as $value){ if($value === null || $value === ''){ return true; } } return false; } $arr1 = array('Tom', 'Jerry', 'Kate'); $arr2 = array('Tom', '', 'Kate'); if(hasEmptyValue($arr1)){ echo '数组$arr1存在空值'; }else{ echo '数组$arr1不存在空值'; } if(hasEmptyValue($arr2)){ echo '数组$arr2存在空值'; }else{ echo '数组$arr2不存在空值'; }
The output result is:
There is no null value in array $arr1
There is a null value in array $arr2
4. Use the array_search() function
The array_search() function is also a very useful PHP built-in function. This function is used to find a value in an array and return the key name corresponding to the value. If the value does not exist, returns FALSE. When determining whether there are null values in the array, we can use the array_search() function to find the key names where all null values are located. Returns TRUE if a null value exists. The following is a code example that uses the array_search() function to determine whether there is a null value in the array:
function hasEmptyValue($arr){ $empty_keys = array_keys($arr, NULL, true); $empty_keys = array_merge($empty_keys, array_keys($arr, '', true)); if(count($empty_keys) > 0){ return true; }else{ return false; } } $arr1 = array('Tom', 'Jerry', 'Kate'); $arr2 = array('Tom', '', 'Kate'); if(hasEmptyValue($arr1)){ echo '数组$arr1存在空值'; }else{ echo '数组$arr1不存在空值'; } if(hasEmptyValue($arr2)){ echo '数组$arr2存在空值'; }else{ echo '数组$arr2不存在空值'; }
The output result is:
There is no null value in array $arr1
There is a null value in array$arr2
Summary:
Using the in_array() function, empty() function, foreach() function and array_search() function can all determine whether an array contains a null value. In actual programming, we can choose different methods according to specific situations. No matter which method is used, you should be proficient in array-related functions and syntax. After many times of practice, you can master the operation and processing skills of arrays in PHP.
The above is the detailed content of PHP determines whether there is a null value in an array. For more information, please follow other related articles on the PHP Chinese website!