按属性过滤数组

时间:2016-02-10 15:21:34

标签: php arrays filter

如何在PHP中过滤此数组,使其仅包含[is_published] = 1

的条目

我整个下午一直在寻找Stack Overflow并试过filter_array和其他方法,但我无法做到正确......

FB_manager_array2: Array
(
    [0] => Array
        (
            [data] => Array
                (
                    [0] => Array
                        (
                            [id] => 244987165675334
                            [is_published] => 1
                            [name] => Zitecraft.com
                        )

                    [1] => Array
                        (
                            [id] => 437060646380356
                            [is_published] => 1
                            [name] => Beauty & care Alexandra
                        )

                    [2] => Array
                        (
                            [id] => 663302210366640
                            [is_published] => 1
                            [name] => Tiramisu Gelato ijssalon
                        )

                    [3] => Array
                        (
                            [id] => 295426223953905
                            [is_published] => 1
                            [name] => Ck's Hairstyling
                        )

                    [4] => Array
                        (
                            [id] => 213820525062
                            [is_published] => 1
                            [name] => Citynumbers.com - Feel like going out tonight?
                        )

                    [5] => Array
                        (
                            [id] => 114373308748798
                            [is_published] => 1
                            [name] => YOLO: You Only Live Once
                        )

                    [6] => Array
                        (
                            [id] => 200408800066160
                            [is_published] => 
                            [name] => Citynumbers Social Widget
                        )

                )

            [paging] => Array
                (
                    [cursors] => Array
                        (
                            [before] => MjQ0OTg3MTY1Njc1MzM0
                            [after] => MjAwNDA4ODAwMDY2MTYw
                        )

                )

        )

)

1 个答案:

答案 0 :(得分:1)

使用array_filter

$FB_manager_array2[0]['data'] = array_filter($FB_manager_array2[0]['data'], function($v) {
    return $v['is_published'] == '1';
});