使用PHP中的条件过滤数组

时间:2016-11-13 14:08:51

标签: php arrays filter

我正在尝试过滤一个数组,以便在PHP中输出所有将在假日范围内(即2016年11月14日至2016年11月18日)假期的孩子。

我在数组中得到以下数据,但我对数据的创建没有任何控制。

[{"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"}]

我只需输出那些"假期"是> = 14-11-2016且< = 18-11-2016。

我尝试了以下逻辑,但它没有返回任何内容

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

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

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

2 个答案:

答案 0 :(得分:3)

如果您确定'holiday'字段包含正确的数据,那么您可以使用以下代码:

$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
}

您还可以比较DateTime对象:

//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 );
}

答案 1 :(得分:1)

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/>";
}

使用PHP 5.3及更高版本的闭包:

$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);
});