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
        )
)

たとえば、月の名前を使用して配列をフィルター処理したい: 3 月、次のようなものを取得します。

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 に答える