使用键过滤多维数组

时间:2019-02-19 01:22:07

标签: php arrays object filter

我需要那里的PHP专家的帮助。我想对此结果进行一些过滤。我想使用键custom_search_emp_id_11对其进行过滤。希望有一个较短的/一个班轮代码/功能来做过滤器?预先感谢您的回答!

Array
(
    [0] => stdClass Object
        (
            [custom_search_emp_id_11] => flag_emp_id_11
            [custom_search] => flag_emp_id_11
            [eti_id] => 1
            [time] => 1:00
            [emp_id] => 11
        )
    [1] => stdClass Object
        (
            [custom_search_emp_id_22] => flag_emp_id_22
            [custom_search] => flag_emp_id_22
            [eti_id] => 4
            [time] => 1:00
            [emp_id] => 22
        )
    [2] => stdClass Object
        (
            [custom_search_emp_id_33] => flag_emp_id_33
            [custom_search] => flag_emp_id_33
            [eti_id] => 5
            [time] => 1:00
            [emp_id] => 33
        )
    [3] => stdClass Object
        (
            [custom_search_emp_id_11] => flag_emp_id_11
            [custom_search] => flag_emp_id_11
            [eti_id] => 1
            [time] => 1:00
            [emp_id] => 11
        )
)

,输出将是:

Array
(
    [0] => stdClass Object
        (
            [custom_search_emp_id_11] => flag_emp_id_11
            [custom_search] => flag_emp_id_11
            [eti_id] => 1
            [time] => 1:00
            [emp_id] => 11
        )

    [3] => stdClass Object
        (
            [custom_search_emp_id_11] => flag_emp_id_11
            [custom_search] => flag_emp_id_11
            [eti_id] => 2
            [time] => 1:00
            [emp_id] => 11
        )
)

1 个答案:

答案 0 :(得分:1)

您可以将array_filter与用于检查对象中custom_search_emp_id_11键是否存在的功能一起使用:

$filtered_array = array_filter($array, function ($v) { return isset($v->custom_search_emp_id_11); });

Demo on 3v4l.org