0

我正在尝试过滤 PHP 中的数组。请看我的数组:

Array
(
    [0] => Array
        (
            [date_start_month_name] => January
            [approved] => Accepted: yes
        )

    [1] => Array
        (
            [date_start_month_name] => March
            [approved] => Accepted: yes
        )

    [2] => Array
        (
            [date_start_month_name] => March
            [approved] => Accepted: yes
        )

    [3] => Array
        (
            [date_start_month_name] => April
            [approved] => Accepted: yes
        )
)

我想使用月份名称过滤数组,例如:March并得到如下内容:

Array
(
    [0] => Array
        (
            [date_start_month_name] => March
            [approved] => Accepted: yes
        )

    [1] => Array
        (
            [date_start_month_name] => March
            [approved] => Accepted: yes
        )
)

我怎样才能做到这一点?提前感谢您的帮助。最好的!

4

2 回答 2

2
$march = array_filter($my_arr, function($value) { 
   return $value['date_start_month_name'] == "March"; 
});
于 2013-02-06T10:39:06.197 回答
1

尝试array_filter

$array=array(array("month"=>"January","foo"=>"bar"),array("month"=>"January","foo"=>"bar"),array("month"=>"March","foo"=>"bar"),array("month"=>"March","foo"=>"bar"));
print_r($array);
$result=array_filter($array,function($o){return $o["month"]=="March";});
print_r($result);

输出:

Array
(
    [0] => Array
        (
            [month] => January
            [foo] => bar
        )

    [1] => Array
        (
            [month] => January
            [foo] => bar
        )

    [2] => Array
        (
            [month] => March
            [foo] => bar
        )

    [3] => Array
        (
            [month] => March
            [foo] => bar
        )

)
Array
(
    [2] => Array
        (
            [month] => March
            [foo] => bar
        )

    [3] => Array
        (
            [month] => March
            [foo] => bar
        )

)
于 2013-02-06T10:40:36.517 回答