Code _ PHP tips for determining whether the php array is empty

Source: Internet
Author: User
These two methods can be used to determine whether a simple array or multi-dimensional array is null. In general, empty () is used to determine whether the array is not empty. This makes the code easier to understand. PHP preferred method for determining null array: count ($ arr), size ($ arr );

The code is as follows:


$ Arr = array ("");
Echo count ($ arr );
Echo size ($ arr );
// Output 1


The code is as follows:


$ Arr = array ();
Echo count ($ arr );
Echo size ($ arr );
// Output 0



PHP method 2 for null array determination: empty ($ arr );

The code is as follows:


$ Arr = array ("");
$ Result = empty ($ arr );
// $ Result = false
$ Arr = array ();
$ Result = empty ($ arr );
// $ Result = true



These two methods can be used to determine whether a simple array or multi-dimensional array is null. In general, empty () is used to determine whether the array is not empty. This makes the code easier to understand.

Determine whether the array is empty. use this function. if the count output is 0, the array is empty. below is the simple test code. Php determines whether the array is a null code column

The code is as follows:


<? Php
$ Arr = array ();
Echo count ($ arr );
?>

If the output is 0, the array is empty.

PHP method 2 for null array determination: empty ($ arr );

The code is as follows:


$ Arr = array ("");
$ Result = empty ($ arr );
// $ Result = false
$ Arr = array ();
$ Result = empty ($ arr );
// $ Result = true

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?

PHP judges whether the array is null 2 and implode ();

Use implode () to output the array as a string and determine whether the output string is null. It seems to be a good method at the beginning, but unfortunately it keeps up with 1.1. it won't work for arrays above two dimensions. For example:

The code is as follows:


$ Arr = array (), array (), arr (www.jb51.net) ay ());
$ Str = implode (',', $ arr );
If (empty ($ str) echo "null ";
Else echo "not empty ";

Obviously, $ arr is a two-dimensional array containing three empty arrays. it should be empty, but the output is indeed non-empty. Failed.
PHP judges the array as null 3, count ();

The code is as follows:


$ Arr = array ("","","");
Echo count ($ arr );

PHP judges that the array is null. 4. in_array ('', $ arr ));

The code is as follows:


$ Arr = array ("d", "s ","");
Echo in_array ('', $ arr );

This can only indicate empty elements in the array, and it cannot be proved that the array is empty. Obviously not.

PHP determines if the array is null. 5. empty ();

This cpyeh is similar to the previous methods.

The code is as follows:


$ Arr = array ("","","");
If (empty ($ arr) echo "null ";
Else echo "not empty ";

The result is not empty.

PHP judges that the array is null. use strlen (). if there is no content, it seems that the length is 1.
You can also add print_r ($ arr) in the preceding example.

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.