Filtering Empty Values in Array

Hi guys, I’m trying to filter an array which contains all of my POST values from a form that I have put into a session. Trying to filter out empty values but just returning the same values which are empty.

$details = array_filter($_SESSION['DETAILS']);
		print_r($details);

See below of my output:

Array ( [ctitle] => Mrs [cfirstname] => Joe [clastname] => Bloggs [caddress] => 23 Joe Blogg Street, Devon [ccontactnumber] => 01234 039213 [cemail] => coxdabd@gmail.com [receiptno] => KK3 [delmethod] => NULL [products] => Array ( [0] => Array ( [name] => Test Product [serial] => 3929 [stock] => [quantity] => 1 [price] => 899 ) [1] => Array ( [name] => [serial] => [stock] => [quantity] => [price] => ) [2] => Array ( [name] => [serial] => [stock] => [quantity] => [price] => ) [3] => Array ( [name] => [serial] => [stock] => [quantity] => [price] => ) [4] => Array ( [name] => [serial] => [stock] => [quantity] => [price] => ) [5] => Array ( [name] => [serial] => [stock] => [quantity] => [price] => ) [6] => Array ( [name] => [serial] => [stock] => [quantity] => [price] => ) ) [submit] => Submit )

Array_filter isn’t recursive, so it doesn’t reach those nested arrays. :slight_smile:

See this User Contributed Note over at the manual page for [fphp]array_filter[/fphp].

Oh :frowning: Damn, that’s annoying! Anything I can do instead? :smiley:

Hi there,

Try this snippet from http://php.net/manual/en/function.array-filter.php :slight_smile:

<?php 
  function array_filter_recursive($input) 
  { 
    foreach ($input as &$value) 
    { 
      if (is_array($value)) 
      { 
        $value = array_filter_recursive($value); 
      } 
    } 
    
    return array_filter($input); 
  } 
?> 

HTH.

Hey guys, that’s perfect. That’s done it perfectly. Cheers for your help :smiley:

No probs, glad it helped :slight_smile:

Bear in mind what array_filter removes though…


<?php
$fixture = array(
  null,
  'null',
  '',
  0,
  '0',
  false,
  'false'
);

print_r(
  array_filter($fixture)
);

/*
  Array
  (
      [1] => null
      [6] => false
  )
*/

Hey bud, sure, think I understand things now. Thanks for your help as always! Appreciate it! :smiley: