0

我有 2 个数组,我需要查找数组 1 中的值之一是否与数组 2(多维数组)中的值之一匹配。我还需要检查数组一中的值是否在数组二中的特定键中,作为“权限”键的“主”键也可能包含该值。

这是数组一:

Array
(
    [0] => 17
    [1] => 6
    [2] => 3
    [3] => 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(){}}} 但它看起来非常混乱且效率低下。在解决这个问题时遇到了一些麻烦。有任何想法吗?

4

2 回答 2

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);
于 2013-04-09T00:27:06.207 回答
1

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

于 2013-04-09T00:32:02.560 回答