PHP determines whether the Array (multi-dimensional array) value is null-PHP source code

Source: Internet
Author: User
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;
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.