求教 多维数组中删除空记录 (上次解决的有个bug

WBOY
Release: 2016-06-23 13:38:26
Original
1005 people have browsed it

原贴
:http://bbs.csdn.net/topics/390997712



最新疑问,当数组里有一个是 ‘0’ 的值时,会把这个元素删除掉,而实际上这是一条有值的元素
["status"]=>
    string(1) "0"




$a = array(
  array('a' => 0, 'b'=> ''),
  array('a' => 0, 'b'=> ''),
  array('a' => 1, 'b'=> '2'),
);
print_r(array_values(array_no_empty($a)));
 
function array_no_empty($arr) {
  if (is_array($arr)) {
    foreach ( $arr as $k => $v ) {
      if (empty($v)) unset($arr[$k]);
      elseif (is_array($v)) {
        $t = array_no_empty($v);
        if($t) $arr[$k] = $t;
        else unset($arr[$k]);
      }
    }
  }
  return $arr;
}
这个方法再怎么改进,谢谢!


回复讨论(解决方案)

if( empty() )    改为  if($v === false){ unset .......... }

if( empty() )    改为  if($v === false){ unset .......... }




不太好使, 这样‘0’的值保存下来了

可是 有空字符串的数据没删除掉

 if (empty($v)) unset($arr[$k]);
改为
 if (empty($v)  && $v !== 0) unset($arr[$k]);



多谢 
       && $v !== ‘0’

好像还有点问题 我再看看

对着呢  。 别的模块调用有点问题

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!