在数组中搜索N级嵌套=在数组中递归搜索(在键上)

时间:2017-09-26 16:52:47

标签: php arrays search recursion data-structures

这是一个数组。一旦新元素与parent_uuid一起出现,我需要将其添加到相应的位置,即具有uuid值为parent_uuid值的项目的子元素。然后孩子可以有其他孩子,如果指定了,我需要将其插入特定的父母。我想为此我需要使用parent_uuid值搜索多维数组。我该怎么做并插入PHP?

Array
(
    [0] => Array
        (
            [id] => 1
            [uuid] => ef4b72ae-012a-4b2c-88b2-d4bf8726fcb9
            [parent_uuid] => 
            [name] => First Parent
            [children] => Array
                (
                )
        )

    [1] => Array
        (
            [id] => 2
            [uuid] => 74bd4b37-6a20-4579-99a3-ce56b0bc28a7
            [parent_uuid] => 
            [name] => Second Parent
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 3
                            [uuid] => f87c6d5c-93ec-40bf-a04d-c925dd1e0aca
                            [parent_uuid] => 74bd4b37-6a20-4579-99a3-ce56b0bc28a7
                            [name] => First Child
                            [children] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [id] => 4
                            [uuid] => cb2b3d9d-867c-40a0-9254-05b466859db1
                            [parent_uuid] => 74bd4b37-6a20-4579-99a3-ce56b0bc28a7
                            [name] => Second Child
                            [children] => Array
                                (
                                )

                        )

                )

        )

)

2 个答案:

答案 0 :(得分:1)

这是你需要的结构

$Array["ef4b72ae-012a-4b2c-88b2-d4bf8726fcb9"]['name'] = "First Parent";
$Array["ef4b72ae-012a-4b2c-88b2-d4bf8726fcb9"]['children'] = [];

$Array["74bd4b37-6a20-4579-99a3-ce56b0bc28a7"]['name'] = "Second Parent";
$Array["74bd4b37-6a20-4579-99a3-ce56b0bc28a7"]['children']["f87c6d5c-93ec-40bf-a04d-c925dd1e0aca"]['name'] = "First Child";
$Array["74bd4b37-6a20-4579-99a3-ce56b0bc28a7"]['children']["f87c6d5c-93ec-40bf-a04d-c925dd1e0aca"]['children'] = [];

$Array["74bd4b37-6a20-4579-99a3-ce56b0bc28a7"]['children']["cb2b3d9d-867c-40a0-9254-05b466859db1"]['name'] = "Second Child";
$Array["74bd4b37-6a20-4579-99a3-ce56b0bc28a7"]['children']["cb2b3d9d-867c-40a0-9254-05b466859db1"]['children'] = [];

如果您确实需要'名称'或任何需要与每个项目存储的补充信息。如果它只是一个uid的树结构,摆脱'name'和'children'键

还没有找到标准的php函数来递归搜索给定的密钥(任何人?)

所以这是你需要的功能

    function insertItem($newItem,$uidParent,$array) {
    foreach ($array as $uid => $content) {
        if ($uid == $uidParent) { // parent found, item insert
            $array[$uid]['children'][$newItem['uid']]['name'] = $newItem['name'];
            $array[$uid]['children'][$newItem['uid']]['children'] = [];
        } elseif (!empty($content['children'])) { // recursively search the tree
            $array[$uid]['children'] = insertItem($newItem,$uidParent,$content['children']);
        }
    }
    return $array;
}

$newItem['name'] = "new item";
$newItem['uid'] = "f87c6d5c-93ec-40bf-a04d-c925dd1e0aca";
$uidParent = "cb2b3d9d-867c-40a0-9254-05b466859db1";
$Array = insertItem($newItem,$uidParent,$Array);

sandbox here

答案 1 :(得分:1)

我认为你需要某种递归功能,这是我凌乱的例子。

<angular2-fullcalendar *ngIf="calendarOptions"
          [options]="calendarOptions" 
          (initialized)="onCalendarInit($event)"></angular2-fullcalendar>