将子项添加到多维数组中

时间:2016-12-08 16:22:31

标签: php recursion multidimensional-array

我有一个多维数组,其树状结构为parent =>儿童。我想将子节点添加到最后一个数组节点,但我无法弄清楚如何。下面我有我的阵列和我到目前为止的代码片段,我只是卡住了

sync.php

这是我的递归遍历函数。任何帮助表示赞赏。谢谢!

  Array
    (
        [id] => 154
        [text] => root
        [parent_id] => 
        [children] => Array
            (
                [155] => Array
                    (
                        [id] => 155
                        [text] => DE
                        [parent_id] => 154
                        [children] => Array
                            (
                                [157] => Array
                                    (
                                        [id] => 157
                                        [text] => Mülheim
                                        [parent_id] => 155
                                        [children] => Array
                                            (
                                            )        
                                    )

                                [158] => Array
                                    (
                                        [id] => 158
                                        [text] => Heißen
                                        [parent_id] => 155
                                        [children] => Array
                                            (
                                            )        
                                    )

                                [159] => Array
                                    (
                                        [id] => 159
                                        [text] => Essen
                                        [parent_id] => 155
                                        [children] => Array
                                            (
                                            )        
                                    )        
                            )        
                    )

                [156] => Array
                    (
                        [id] => 156
                        [text] => RO
                        [parent_id] => 154
                        [children] => Array
                            (
                                [160] => Array
                                    (
                                        [id] => 160
                                        [text] => Alba Iulia
                                        [parent_id] => 156
                                        [children] => Array
                                            (
                                            )        
                                    )

                                [161] => Array
                                    (
                                        [id] => 161
                                        [text] => Sibiu
                                        [parent_id] => 156
                                        [children] => Array
                                            (
                                            )        
                                    )        
                            )        
                    )        
            )        
    )

2 个答案:

答案 0 :(得分:0)

您可以遍历数组以获取底层对象,然后从那里向上移动并向该数组添加元素。

$previousNode = null;
$node = //Top Level Object
while(sizeof($node->$children) > 0)
{
     $prevNode = $node;
     $node = $children[0];
}

//Switch to up one level
$node = previousNode;

//Add children like 
array_push($node->$children, //element);

答案 1 :(得分:0)

/**
 * Add array into structure with some child id.
 * @param int $child_id Child id
 * @param array $structure Initial structure.
 * @param array $add Add child array.
 */
function add_to_node($child_id, & $structure, $add) {
    foreach( $structure as & $data ) {
        if( isset($data['id']) ) {
            if( $data['id'] == $child_id ) {
                $data['children'][ $add['id'] ] = $add;
                break;
            }
        }

        if( isset($data['children']) ) {
            add_to_node($child_id, $data['children'], $add);
        }
    }
}

// Initial array structure.
$data = array(
    1 => array(
        'id' => 1,
        'children' => array(
            12 => array(
                'id' => 12,
                'children' => array(),
            ),

            15 => array(
                'id' => 15,
                'children' => array(
                    55 => array(
                        'id' => 55,
                    ),
                ),
            ),
        ),
    ),
);

// This code will add new child with ID 44 to parent with id 55.
add_to_node(55, $data, array(
    'id' => 44,
    'text' => 'my text',
));

print_r($data);