Home  >  Article  >  Backend Development  >  PHP program code to determine empty array_PHP tutorial

PHP program code to determine empty array_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:15:421048browse

Many friends ask how to judge hollow arrays in PHP. Let me summarize some commonly used program codes for judging empty arrays. Friends who need to know can enter for reference.

empty function, this is to determine whether the array value is empty

The code is as follows
 代码如下 复制代码

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

Copy code


$arr= array(""); $result = empty($arr); //$result = false

$arr = array();

$result = empty($arr);
 代码如下 复制代码

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

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

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

else echo "非空";

//$result = true


 代码如下 复制代码


$str1_array=array('一聚教程网','','http://www.bKjia.c0m','','1654','');
$str1_array=array_filter($str1_array);
print_r($str1_array);
?>

结果

Array
(
[0] => 一聚教程网
[2] => http://www.bKjia.c0m
[4] => 1654
)

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:

if(empty($str)) echo "empty";
The code is as follows Copy code



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

$str = implode(',',$arr);
else echo "not empty";

array_filter function
The code is as follows Copy code
$str1_array=array('Yiju Tutorial Network','','http://www.bKjia.c0m','','1654',''); $str1_array=array_filter($str1_array);

print_r($str1_array); ?>
Results Array ( [0] => Yiju Tutorial Network [2] => http://www.bKjia.c0m [4] => 1654 ) count,size() function These two functions are method codes for judging empty arrays $arr= array(""); echo count($arr); echo size($arr); //Output 1 http://www.bkjia.com/PHPjc/628799.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628799.htmlTechArticleMany friends ask how to judge hollow arrays in PHP. Let me summarize some commonly used program codes for judging empty arrays. Friends who need to know can enter for reference. empty function, this is the judgment...
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