如何循环遍历多层数组并替换其中的一些关联值



如何通过多层数组循环并替换其中的一些关联值?

例如,这是数组

$items = array(
    0 => array( 
        "id" => "1",
        "title" => "parent 1",
        "children" => array()
        ),
    1 => array( 
        "id" => "2",
        "title" => "parent 2",
        "children" => array (
           0 => array( 
            "id" => "4",
            "title" => "children 1",
               "granchildren" => array(
                    0 => array( 
                        "id" => "7",
                        "title" => "granchildren 1"
                    ),
                   1 => array( 
                        "id" => "8",
                        "title" => "granchildren 2"
                    )
               )
            ),
           1 => array( 
            "id" => "5",
            "title" => "children 2",
            "granchildren" => array()
            ) 
        ),
        ),
    3 => array( 
        "id" => "3",
        "title" => "parent 3",
        "children" => array()
        )
);

我有两个工作函数,

    function translate ($id){
        $items = array(
            0 => array(
                "id" => 1,
                "title" => "parent 1 en"
            ),
            1 => array(
                "id" => 4,
                "title" => "children 1 en"
            ),
            2 => array(
                "id" => 8,
                "title" => "granchildren 2 en"
            )
        );
        foreach($items as $item) {
            if($id === $item['id'])
            {
                return $item['title'];
            }
        }
    }
function looper ($items){
    $new_items = array();
    foreach($items as $key => $item) {
        if(isset($key) && is_array($key)){
           $new_items[$key] = translate($item['id']);
        }else {
            //looper($item);
        }
    }
    return $new_items;
}
print_r(looper ($items));

这是我想要的结果,

Array
(
    [0] => Array
        (
            [id] => 1
            [title] => parent 1 en // translated
            [children] => Array
                (
                )
        )
    [1] => Array
        (
            [id] => 2
            [title] => parent 2
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 4
                            [title] => children 1 en // translated
                            [granchildren] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 7
                                            [title] => granchildren 1
                                        )
                                    [1] => Array
                                        (
                                            [id] => 8
                                            [title] => granchildren 2 en // translated
                                        )
                                )
                        )
                    [1] => Array
                        (
                            [id] => 5
                            [title] => children 2
                            [granchildren] => Array
                                (
                                )
                        )
                )
        )
    [3] => Array
        (
            [id] => 3
            [title] => parent 3
            [children] => Array
                (
                )
        )
)

有可能吗?

听起来像是array_walkarray_walk_recursive的工作。
它将为数组中的每个项调用用户提供的函数。你可以让它通过引用来修改数组来达到你想要的效果。

相关内容

  • 没有找到相关文章

最新更新