PHP checks whether the array (multi-dimensional array) value is null

Source: Internet
Author: User

// 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: Copy code

// 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: Copy code

<? Php
// 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: Copy code

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;
}
}
}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.