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



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

示例数组:

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个级别?

在一些帮助下,我已经设法使它工作了:

    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;
}

相关内容

  • 没有找到相关文章