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

3 Answers3

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!='';
});
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
0
  $newArr = [];
  foreach($myArr as $key => $val){
      if(!empty($val)){
          $newArr[$key] = $val;
      }

  }
informer
  • 821
  • 6
  • 18
  • 2
    Thank you for this code snippet, which may provide some immediate help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) 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. – Toby Speight Jul 19 '17 at 12:00
0

Use array_filter() function it will remove

array_filter($array);
Suneel Kumar
  • 1,650
  • 2
  • 21
  • 31