The dateevent field contains values ​​in the Ymd format. When filtering, I try to substitute the $dattw variable with the current date in order to unload all values ​​that are larger than the current date from the array.

The filter does not work and unloads all the values if, instead of $dattw variable, I directly enter the value 2015-11-30 filter safely works, unloading the dates I need. Help where is the error?

 <?php $dattw = date("Ymd"); $mykey_values = get_post_custom_values('dateevent'); foreach ( $mykey_values as $key => $value ) $filteredarray = array_filter ($mykey_values, function ($item) { return ($item >$dattw); }); sort ($filteredarray); foreach ($filteredarray as $filteredarray => $value) echo "$value<br>"; ?> 
  • 2
    and you conclude $ dattw (in the most anonymous function) and see what it is equal to (or not equal to). And then read about anonymous functions , then you will understand why $ dattw is absent there - BOPOH
  • @BOPOH I recommend to postpone it in response (since de facto this is it) - tutankhamun

1 answer 1

An anonymous function knows nothing about the $dattw variable. Add use ($dattw) :

 $ filteredarray = array_filter ($ mykey_values, function ($ item) use ($ dattw) {return ($ item> $ dattw);});

In addition to comment @RAVON.

  • Thanks, it helped. - Eugene