2

I have an array which have null and 0 values.So I want to remove null and 0 from array

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
)

I am trying to delete null values and 0 from array.so I tried

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

I got output like

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

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

Still some field remain.Please help me

4
  • possible duplicate of: stackoverflow.com/questions/3654295/remove-empty-array-elements Commented Jul 19, 2017 at 6:28
  • array_filter() Commented Jul 19, 2017 at 6:28
  • test stack array_filter() working perfectly fine Commented Jul 19, 2017 at 6:32
  • 1
    May be value is not null or blank but containing white spaces Commented Jul 19, 2017 at 6:33

3 Answers 3

1

You might be able to do something like this - invoke a custom function for each element in the array to see if it passes your criteria

$values=array_filter($_POST,function( $item ){
    return !is_null( $item ) && !empty( $item ) && strlen( trim( $item ) ) > 0 && $item!='';
});
Sign up to request clarification or add additional context in comments.

Comments

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

  }

1 Comment

Thank you for this code snippet, which may provide some immediate help. A proper explanation would greatly improve its educational value by showing why this is a good solution to the problem, and would make it more useful to future readers with similar, but not identical, questions. Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.
0

Use array_filter() function it will remove

array_filter($array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.