找到一个价值&键入多维数组

时间:2013-04-09 00:14:42

标签: php arrays

我有2个数组,我需要找到数组1中的一个值是否匹配数组2中的一个值,即多维数组。我还需要检查数组1中的值是否在数组2中的特定键中,“principal”键作为“权限”键也可以保存此值。

这里是数组一:

Array
(
    [0] => 17
    [1] => 6
    [2] => 3
    [3] => 2
)

和数组2 [为了便于阅读,实际上略微截断]:

Array
(
    [modAccessResourceGroup] => Array
        (
            [3] => Array
                (
                    [0] => Array
                        (
                            [principal] => 0
                            [authority] => 9999
                            [policy] => Array
                                (
                                    [load] => 1
                                )

                        )

                    [1] => Array
                        (
                            [principal] => 2
                            [authority] => 10
                            [policy] => Array
                                (
                                    [add_children] => 1
                                    [create] => 1
                                    [copy] => 1
                                    [delete] => 1
                                    [list] => 1
                                    [load] => 1
                                    [move] => 1
                                    [publish] => 1
                                    [remove] => 1
                                    [save] => 1
                                    [steal_lock] => 1
                                    [undelete] => 1
                                    [unpublish] => 1
                                    [view] => 1
                                )

                        )

                    .... truncated ....

                    [13] => Array
                        (
                            [principal] => 16
                            [authority] => 9999
                            [policy] => Array
                                (
                                    [load] => 1
                                )

                        )

                )

            [8] => Array
                (
                    [0] => Array
                        (
                            [principal] => 0
                            [authority] => 9999
                            [policy] => Array
                                (
                                    [load] => 1
                                )

                        )

                    [1] => Array
                        (
                            [principal] => 1
                            [authority] => 9999
                            [policy] => Array
                                (
                                    [add_children] => 1
                                    [create] => 1
                                    [copy] => 1
                                    [delete] => 1
                                    [list] => 1
                                    [load] => 1
                                    [move] => 1
                                    [publish] => 1
                                    [remove] => 1
                                    [save] => 1
                                    [steal_lock] => 1
                                    [undelete] => 1
                                    [unpublish] => 1
                                    [view] => 1
                                )

                        )

                    [2] => Array
                        (
                            [principal] => 22
                            [authority] => 9999
                            [policy] => Array
                                (
                                    [add_children] => 1
                                    [create] => 1
                                    [copy] => 1
                                    [delete] => 1
                                    [list] => 1
                                    [load] => 1
                                    [move] => 1
                                    [publish] => 1
                                    [remove] => 1
                                    [save] => 1
                                    [steal_lock] => 1
                                    [undelete] => 1
                                    [unpublish] => 1
                                    [view] => 1
                                )

                        )

                )

        )

) 

我使用了一系列foreach(){foreach(){foreach(){}}}但它看起来非常混乱且效率低下。在解决这个问题时遇到一些麻烦。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

递归函数应该可以解决这个问题:

$values = array(17, 6, 3, 2, 5);

function find($array, &$values) {
    foreach ($array as $key => $element) {
        if (is_array($element)) {
            find($element, $values);
        }

        elseif ($key == 'principal') {
            foreach ($values as $value) {
                if ($element == $value) {
                    echo 'Found' . PHP_EOL;
                    // Do stuff
                }
            }
        }
    }
}

find($array, $values);

答案 1 :(得分:1)

有些事情会浮现在脑海中。首先,在这种情况下,我通常会创建一个只包含主值的单独数组,这样我就可以遍历第一个数组,只需使用简单的in_array()检查。其次,如果你不想这样做,你可以使用array_walk_recursive()函数或array_search()中的一些递归示例来完成第二个数组。