snowflakes74
snowflakes74

Reputation: 1307

Filtering an array with condition in PHP

I am trying to filter an array to output all the children who will be on holidays within a date range (i.e 14-11-2016 till 18-11-2016) in PHP.

I get the following data in an array and I do not have any control on creation of the data.

[{"holiday":"15-11-2016","name":"Josh Stevens"},{"holiday":"17-11-2016","name":"Josh Stevens"},{"holiday":"22-11-2016","name":"Josh Stevens"},{"holiday":"14-11-2016","name":"Naomi Christ"},{"holiday":"15-11-2016","name":"Naomi Christ"},{"holiday":"16-11-2016","name":"Naomi Christ"},{"holiday":"17-11-2016","name":"Naomi Christ"},{"holiday":"14-11-2016","name":"Jasmine Auger"},{"holiday":"15-11-2016","name":"Jasmine Auger"},{"holiday":"16-11-2016","name":"Jasmine Auger"},{"holiday":"17-11-2016","name":"Jasmine Auger"}]

I need to output data for only the ones whose "holiday" are >= 14-11-2016 and <= 18-11-2016.

I have tried the following logic but it does not return anything

function filterArray($value)
{
  return ($value > '14-11-2016');
}

$filteredArray = array_filter($fullArray, 'filterArray');

foreach($filteredArray as $k => $v)
{
 echo "$k = $v";
}

Upvotes: 1

Views: 654

Answers (2)

Dez
Dez

Reputation: 5838

function filterArray($value)
{
  return (strtotime('18-11-2016') >= strtotime($value["holiday"])) && (strtotime($value["holiday"]) >= strtotime('14-11-2016'));
}
$string = '[{"holiday":"15-11-2016","name":"Josh Stevens"},{"holiday":"17-11-2016","name":"Josh Stevens"},{"holiday":"22-11-2016","name":"Josh Stevens"},{"holiday":"14-11-2016","name":"Naomi Christ"},{"holiday":"15-11-2016","name":"Naomi Christ"},{"holiday":"16-11-2016","name":"Naomi Christ"},{"holiday":"17-11-2016","name":"Naomi Christ"},{"holiday":"14-11-2016","name":"Jasmine Auger"},{"holiday":"15-11-2016","name":"Jasmine Auger"},{"holiday":"16-11-2016","name":"Jasmine Auger"},{"holiday":"17-11-2016","name":"Jasmine Auger"}]';

$fullArray = json_decode($string, true);
$filteredArray = array_filter($fullArray, 'filterArray');

foreach($filteredArray as $k => $v)
{
 echo "$k = ". $v['holiday'] ." - ".$v['name']."<br/>";
}

And using closures from PHP 5.3 and upper versions:

$from = strtotime('14-11-2016');
$to = strtotime('18-11-2016');
$string = '[{"holiday":"15-11-2016","name":"Josh Stevens"},{"holiday":"17-11-2016","name":"Josh Stevens"},{"holiday":"22-11-2016","name":"Josh Stevens"},{"holiday":"14-11-2016","name":"Naomi Christ"},{"holiday":"15-11-2016","name":"Naomi Christ"},{"holiday":"16-11-2016","name":"Naomi Christ"},{"holiday":"17-11-2016","name":"Naomi Christ"},{"holiday":"14-11-2016","name":"Jasmine Auger"},{"holiday":"15-11-2016","name":"Jasmine Auger"},{"holiday":"16-11-2016","name":"Jasmine Auger"},{"holiday":"17-11-2016","name":"Jasmine Auger"}]';

$fullArray = json_decode($string, true);
$filteredArray = array_filter($fullArray, function($elem) use ($from, $to){
    $holidayTime = strtotime($elem['holiday']);
    return ($from <= $holidayTime) && ($holidayTime <= $to);
});

Upvotes: 1

olegsv
olegsv

Reputation: 1462

If your are sure that 'holiday' field contains correct data, then you can use this code:

$fullArray = json_decode( '[{"holiday":"15-11-2016","name":"Josh Stevens"},{"holiday":"17-11-2016","name":"Josh Stevens"},{"holiday":"22-11-2016","name":"Josh Stevens"},{"holiday":"14-11-2016","name":"Naomi Christ"},{"holiday":"15-11-2016","name":"Naomi Christ"},{"holiday":"16-11-2016","name":"Naomi Christ"},{"holiday":"17-11-2016","name":"Naomi Christ"},{"holiday":"14-11-2016","name":"Jasmine Auger"},{"holiday":"15-11-2016","name":"Jasmine Auger"},{"holiday":"16-11-2016","name":"Jasmine Auger"},{"holiday":"17-11-2016","name":"Jasmine Auger"}]', true );


//I need to output data for only the ones whose "holiday" are >= 14-11-2016 and <= 18-11-2016.


function filterArray($value)
{
        static $from  = 20161114;
        static $until = 20161118;

        // parse the date and convert it to an int.    
        $v = new \DateTime( $value['holiday'] );
        $v = (int)$v->format('Ymd');

        // compare as integer
        return ($v >= $from && $v <= $until );
}

$filteredArray = array_filter($fullArray, 'filterArray');

foreach($filteredArray as $k => $v)
{
        var_dump($v); // prints sub-array
}

You can also compare DateTime objects:

//I need to output data for only the ones whose "holiday" are >= 14-11-2016 and <= 18-11-2016.

$from  = new \DateTime('2016-11-14');
$until = new \DateTime('2016-11-18');

function filterArray($value)
{
        global $from, $until;
        $v = new \DateTime( $value['holiday'] );
        return ($v >= $from && $v <= $until );
}

Upvotes: 3

Related Questions