从POST数组php中删除空值和0

时间:2017-07-19 06:24:06

标签: php arrays array-filter

我有一个数组,其中包含null和0个值。所以我想从数组中删除null和0

Array
(
    [_token] => cwnTLDn9fhT1UTMDL6e9TxQXdvfoAK74MZjDMjnr
    [datefrom] => 
    [dateto] => 
    [Productivity] =>  
    [Productivityrating] => 0
    [Technical_Skills] =>  
    [Technical_Skillsrating] => 0
    [Work_Consistency] =>  
    [Work_Consistencyrating] => 0
    [Presentation_skills] =>  
    [Presentation_skillsrating] => 0
    [checkvalue] => Array
        (
            [test] => Rejected
        )

    [test] =>  dfdfd
    [testrating] => 0
    [userid] => 
    [userid_giv] => 
    [user] => 
    [submit] => REJECT
)

我试图从array.so中删除空值和0.我试过

$value= array_filter($_POST);
       echo '<pre>';
       print_r($value);exit;

我的输出就像

Array
(
    [_token] => cwnTLDn9fhT1UTMDL6e9TxQXdvfoAK74MZjDMjnr
    [checkvalue] => Array
        (
            [Productivity] => Rejected
        )

    [Productivity] =>  sd
    [Technical_Skills] =>  
    [Work_Consistency] =>  
    [Presentation_skills] =>  
    [test] =>  
    [submit] => REJECT
)

仍然有一些领域。请帮助我

3 个答案:

答案 0 :(得分:1)

你可以做这样的事情 - 为数组中的每个元素调用一个自定义函数,看它是否通过你的标准

$values=array_filter($_POST,function( $item ){
    return !is_null( $item ) && !empty( $item ) && strlen( trim( $item ) ) > 0 && $item!='';
});

答案 1 :(得分:0)

  $newArr = [];
  foreach($myArr as $key => $val){
      if(!empty($val)){
          $newArr[$key] = $val;
      }

  }

答案 2 :(得分:0)

使用它将删除的array_filter()函数

array_filter($array);