The PHP decision array is the Null preferred method: Count ($arr), size ($arr);
Copy Code code as follows:
$arr = Array ("");
echo count ($arr);
Echo size ($arr);
Output 1
Copy Code code as follows:
$arr = Array ();
echo count ($arr);
Echo size ($arr);
Output 0
The PHP judgment array is an empty method 2:empty ($arr);
Copy 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.
It's good to judge whether an array is empty or not. Count if the output is 0 then the array is empty and the following is the simple test code. PHP to determine whether the array is empty code
Copy Code code as follows:
<?php
$arr = Array ();
echo count ($arr);
?>
The array is empty if the output is 0
The PHP judgment array is an empty method 2:empty ($arr);
Copy Code code as follows:
$arr = Array ("");
$result = Empty ($arr);
$result = False
$arr = Array ();
$result = Empty ($arr);
$result = True
PHP judgment array is null one, for loop
The simplest and most straightforward method is to iterate through the array with a for loop. An array of known dimensions can be judged, but what if it is an unknown multidimensional array?
PHP judgment array is null bis, implode ();
Output the array as a string using implode () to determine whether the output is empty. It seems like a good way to start at first, but just like the one, it's not going to be a two-dimensional array. As an example:
Copy Code code as follows:
$arr = Array (array (), array (), arr (www.jb51.net) ay ());
$str = Implode (', ', $arr);
if (empty ($STR)) echo "Empty";
else echo "Non-empty";
Obviously $arr is a two-dimensional array of three empty arrays, which should be empty, but the output is really non-empty. Judgment failed.
The PHP judgment array is empty three, count ();
Copy Code code as follows:
$arr = Array ("", "", "");
echo count ($arr);
PHP judgment array is null four, In_array (', $arr));
Copy Code code as follows:
$arr = Array ("D", "s", "");
Echo In_array (", $arr);
This only means that there are empty elements in the array and that the array is not empty. Obviously not.
PHP judgment array is empty five, empty ();
This Cpyeh feels similar to the previous methods.
Copy Code code as follows:
$arr = Array ("", "", "");
if (empty ($arr)) echo "Empty";
else echo "Non-empty";
The result is still not empty
The PHP judgment array is empty six, uses strlen (), does not have the content words as if the length all is 1
We can also add a print_r to the above example ($arr);