按键找到的多维数组中的更新元素

时间:2015-07-16 07:35:30

标签: php arrays multidimensional-array

也许我不知道如何正确地询问谷歌,但我需要更新多维联合数组中的元素。

示例数组:

Array(

[3853] => Array
    (
        [content_id] => 3853
        [sort_order] => 1
        [level] => 1
    )
[3854] => Array
    (
        [content_id] => 3854
        [sort_order] => 2
        [level] => 1
    )
[4089] => Array
    (
        [content_id] => 4089
        [sort_order] => 3
        [level] => 1
        [children] => Array
            (
                [4083] => Array
                    (
                        [content_id] => 4083
                        [sort_order] => 4
                        [level] => 2
                        [children] => Array
                            (
                                [4082] => Array
                                    (
                                        [content_id] => 4082
                                        [sort_order] => 5
                                        [level] => 3
                                        [children] => Array
                                            (
                                                [3860] => Array
                                                    (
                                                        [content_id] => 3860
                                                        [sort_order] => 6
                                                        [level] => 4
                                                    )
                                            )
                                    )
                            )
                    )
            )
    )
[3917] => Array
    (
        [content_id] => 3917
        [sort_order] => 7
        [level] => 1
        [children] => Array
            (
                [3881] => Array
                    (
                        [content_id] => 3881
                        [sort_order] => 8
                        [level] => 2
                        [children] => Array
                            (
                                [3889] => Array
                                    (
                                        [content_id] => 3889
                                        [sort_order] => 9
                                        [level] => 3
                                    )

                                [3890] => Array
                                    (
                                        [content_id] => 3890
                                        [sort_order] => 10
                                        [level] => 3
                                    )
                            )
                    )
            )
    ) 
)

我想搜索密钥(例如:4082)并添加或更改元素值。

我知道我可以使用array_key_exists()搜索一个键,但只返回true或false。

有没有办法通过引用返回元素而不使用foreach循环,因为我的数组可以有n个级别?

1 个答案:

答案 0 :(得分:0)

通过一些帮助,我设法让它运转起来:

    function &get_element_from_array(&$array, $searchValue){    

    foreach($array as $id => &$subtree) {
        if ($id === $searchValue) {
            return $subtree;
        }

        if (isset($subtree['children'])) {
            $subsearch = &get_element_from_array($subtree['children'], $searchValue);

            if ($subsearch !== false) {
                return $subsearch;
            }
        }
    }

    return false;
}