Home >Backend Development >PHP Tutorial >The specific way to determine if an array is empty in PHP_PHP Tutorial

The specific way to determine if an array is empty in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-15 13:29:30938browse

LearningPHP determines whether an array is empty, for loop

The simplest and most direct method is to use a for loop to traverse the array. It can be judged for arrays with known dimensions, but what should we do if it is an unknown multi-dimensional array?

PHP determines whether the array is empty, implode();

Use implode() to output the array as a string, and determine whether the output string is empty. At first glance, it seems to be a good method, but unfortunately, like the previous point, it does not work for arrays with more than two dimensions. For example:

$arr= array(array(),array(),array());

$str = implode(',',$arr);

if(empty ($str)) echo "empty";

else echo "not empty";

Obviously $arr is a two-dimensional array containing three empty arrays. It should be considered empty, but the output is indeed Right or wrong. Judgment failed.

PHP determines that the array is empty, count();

$arr= array("","","");

echo count($arr );

PHP determines that the array is empty, in_array('', $arr));

$arr= array("d","s","" );

echo in_array('', $arr);

This can only show that there are empty elements in the array, but cannot prove that the array is empty. Obviously not.

PHP determines that the array is empty, empty();

This cpyeh feels similar to the previous methods

$arr= array("","", "");

if(empty($arr)) echo "empty";

else echo "not empty";

The result is still non-empty

PHP determines that the array is Empty 6. Use strlen(). If there is no content, the length seems to be 1

We can also add print_r($arr); to the above example to see.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446357.htmlTechArticleWhen learning PHP, the simplest and most direct way to determine whether an array is empty or a for loop is to use a for loop. Traverse the array. It can be judged for arrays with known dimensions, but if it is an unknown number...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn