The method to determine whether the array value is null is simple. We only need empty or. If it is a two-dimensional array or multi-dimensional array, we can directly use traversal to operate, the following is a summary of the methods for judging whether the array value is null. The method to determine whether the array value is null is simple. We only need empty or = \ "\". If it is a two-dimensional array or multi-dimensional array, we can directly use traversal to operate, the following is a summary of the methods for judging whether the array value is null.
Script ec (2); script
// Here, $ res is an array, but the dimension of the array is not fixed. In this case, the count function cannot be used.
| The Code is as follows: |
|
// Array_filter () will delete all entries in $ res that are equivalent to FALSE $ Filter_res = array_filter ($ res ); If (! Empty ($ filter_res )) { // Operation for which the array is not empty } Else { // Empty array operation } |
PHP judges that the array is null and the for Loop
The simplest and most direct method is to use a for loop to traverse the array. An array with known dimensions can be judged, but what if it is an unknown multi-dimensional array?
(For 2D arrays only)
| The Code is as follows: |
|
// Put the array you want to judge on it. Its name is $ shuzu. Foreach ($ shuzu as $ v1) { Foreach ($ v1 as $ v2) { // Print "$ v2 "; $ Shunzi = $ shunzi. $ v2; } If ($ shunzi = "") { Echo "The array is empty "; } Else { Echo "array element "; } ?> Count ($ arr), size ($ arr ); $ Arr = array (""); Echo count ($ arr ); Echo size ($ arr ); // Output 1
|
Whether the multi-dimensional array value is null
PHP can determine whether a single value is null using the most commonly used empty. However, it is difficult to determine whether an array with a key value or a multi-dimensional array exists, the following is a function in PHP to determine whether an array (multi-dimensional) value is null:
| The Code is as follows: |
|
Class Test { Public static function array_is_null ($ arr = null ){ If (is_array ($ arr )){ Foreach ($ arr as $ k =>$ v ){ If ($ v &&! Is_array ($ v )){ Return false; } $ T = self: array_is_null ($ v ); If (! $ T ){ Return false; } } Return true; } Elseif (! $ Arr ){ Return true; } Else { Return false; } } } |