Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  php  >  正文 php删除数组中的空值或指定值操作

php删除数组中的空值或指定值操作

发布时间:2019-01-21   编辑:www.jquerycn.cn
jquery中文网为您提供php删除数组中的空值或指定值操作等资源,欢迎您收藏本站,我们将为您提供最新的php删除数组中的空值或指定值操作资源
我们介绍关于php中数组的操作,删除数组中指定值或判断数组中是否有值或清除空值操作,有需要的同学可以参考一下。

首先我们来看看关于http://www.111cn.net/phper/29/2dc95be9381b4bb1753083c09fda1a36.htm
用implode()将数组输出为字符串,判断输出的字串是否为空。初看上去似乎是个不错的方法,可惜跟上一点一样,对于二维以上数组就不行了。举个例子:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7476')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7476>

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

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

很明显$arr是个含有三个空数组的二维数组,应该也算是空的,可是输出的确是非空。判断失败。
三、count(); 可参考http://www.111cn.net/w3school/php/func_array_count.htm

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4269')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4269>

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

四、in_array('', $arr)); 函数用法可参考http://www.111cn.net/phper/24/c5b81a8af14b1c0928eea343f59b454a.htm

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5811')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5811>$arr= array("d","s","");
echo in_array('', $arr);

这个只能说明数组中有空的元素,不能证明数组是空的。很明显也不行。
五、empty(); 函数用法可参考http://www.111cn.net/so/php empty()
这个cpyeh觉得跟前面几种方法差不多

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1419')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1419>

$arr= array("","","");
if(empty($arr)) echo "空";
else echo "非空";

结果还是非空
六、用strlen(),没内容的话好象长度都为1

结合上面实例我们写一个完整的删除数组空值的元素

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2113')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2113>

function array_remove_key($array, $keys)
{
$num = count($keys);
$num_last = $num - 1;
$this_array_0 = &$array;
$last_key = $keys[$num_last];
for ($i = 0; $i < $num_last; $i )
{
$this_key = $keys[$i];
$this_var_name = 'this_array_' . $i;
$next_var_name = 'this_array_' . ($i 1);
if (!array_key_exists($this_key, $$this_var_name)) {
break;
}
$$next_var_name = &${$this_var_name}[$this_key];
}
unset(${$next_var_name}[$last_key]);
return $array;
}

您可能感兴趣的文章:
php实现共享内存进程通信函数之shm
PHP删除数组中空值
php 判断数组维数的例子(一维,二维或多维)
PHP 5 Array 函数
php数组字符转换 排序(php教程二)
php 数据组增加,删除,查询,排序详细说明
php删除数组中的空值或指定值操作
php中FTP操作函数大全(功能说明)
jQuery学习7 操作JavaScript对象和集合的函数
【PHP学习】新手必备PHP常用函数大集合

[关闭]