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

PHP program code to determine empty array_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 17:15:42
Original
1147 people have browsed it

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 of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template