Code to determine if the PHP array is empty

Source: Internet
Author: User
PHP judgment Array is empty preferred method: Count ($arr), size ($arr);

Copy the Code code as follows:


$arr = Array ("");
echo count ($arr);
Echo size ($arr);
Output 1


Copy the Code code as follows:


$arr = Array ();
echo count ($arr);
Echo size ($arr);
Output 0


PHP judgment Array is empty method 2:empty ($arr);

Copy the Code code as follows:


$arr = Array ("");
$result = Empty ($arr);
$result = False
$arr = Array ();
$result = Empty ($arr);
$result = True


These two methods are sufficient to deal with the question of whether simple arrays and multidimensional arrays are empty, and individuals generally use empty () to make array non-null judgments, so that the sense code looks easier to understand.

Determine if the array is empty use this function just fine count if the output is 0 then the array is empty the following is a simple test code. PHP to determine if an array is empty code

Copy the Code code as follows:


<?php
$arr = Array ();
echo count ($arr);
?>

If the output is 0, the array is empty.

PHP judgment Array is empty method 2:empty ($arr);

Copy the Code code as follows:


$arr = Array ("");
$result = Empty ($arr);
$result = False
$arr = Array ();
$result = Empty ($arr);
$result = True

PHP judgment array is empty one, for loop

The simplest and most straightforward method is to iterate through the array with a for loop. The array of known dimensions can be judged, but what if it is an unknown multidimensional array?

PHP judgment array is empty two, implode ();

Use implode () to output the array as a string to determine if the output is empty. It seems like a good way to start at first, but as with a little bit more than two-dimensional arrays. As an example:

Copy the Code code as follows:


$arr = Array (array (), array (), arr (www.jb51.net) ay ());
$str = Implode (', ', $arr);
if (empty ($STR)) echo "null";
else echo "non-null";

It is obvious that $arr is a two-dimensional array with three empty arrays, which should also be empty, but the output is indeed non-empty. Judgment failed.
PHP judgment array is empty three, count ();

Copy the Code code as follows:


$arr = Array ("", "", "");
echo count ($arr);

PHP judgment array is empty four, In_array (", $arr));

Copy the Code code as follows:


$arr = Array ("D", "s", "");
Echo In_array (', $arr);

This can only indicate that there are empty elements in the array and cannot prove that the array is empty. It's not obvious.

PHP judgment Array is empty (five);

This Cpyeh thinks it's the same as the previous methods.

Copy the Code code as follows:


$arr = Array ("", "", "");
if (empty ($arr)) echo "null";
else echo "non-null";

The result is still non-empty

PHP judgment array is empty six, with strlen (), no content as if the length is 1
We can also add a sentence print_r ($arr) to the above example;

The above is to determine whether the PHP array is empty code, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • 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.