2017-09-26 27 views
0

這是一個數組。一旦一個新的元素與parent_uuid進來,我需要將其添加到相應的位置,這是該項目的兒童,其uuid值爲parent_uuid值。然後孩子可以有其他孩子,如果這是指定的,我需要將其插入特定的父母。我想爲此我需要使用parent_uuid值搜索多維數組。我怎樣才能做到這一點,並在PHP中插入?在N級嵌套數組中搜索=數組中的遞歸搜索(在鍵上)

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 
           (
           ) 

         ) 

       ) 

     ) 

) 
+3

如果讓步是,「基本上,這是一個樹形結構」,這聽起來像顯而易見的解決方案是實際使用樹,通過實際數據結構所提供的所有搜索的好處。 –

+0

好像這是一個重複的問題 https://codereview.stackexchange.com/questions/44864/recursive-function-filtering-large-multidimensional-array-by-key-element-to-ht –

+0

@ Mike'Pomax 'Kamermans,但如何在PHP中做到這一點? –

回答

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的樹狀結構,擺脫「名」和「孩子」鍵

還沒有找到一個標準的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

我想你需要某種形式的遞歸函數,這裏是我凌亂的例子。

<?php 

header('Content-type: text/plain'); 

$data = 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 (
     ), 
    ), 
    ), 
), 
); 

function arrayAddChild(&$data, $child) { 
    if (!isset($data) || !is_array($data) || empty($data)) { 
     return false; 
    } 
    foreach ($data as $key => $value) { 
     if ($value['uuid'] === $child['parent_uuid']) { 
      $data[$key]['children'][] = $child; 
      return true; 
     } 
     if(arrayAddChild($data[$key]['children'], $child)) { 
      return true; 
     } 
    } 
    return false; 
} 

var_export(arrayAddChild($data, [ 
         'id' => 31, 
         'uuid' => '31', 
         'parent_uuid' => 'cb2b3d9d-867c-40a0-9254-05b466859db1', 
         'name' => 'Second Child', 
         'children' => [] 
         ] 
        )); 

var_export(arrayAddChild($data, [ 
         'id' => 32, 
         'uuid' => '32', 
         'parent_uuid' => '31', 
         'name' => 'Second Child', 
         'children' => [] 
         ] 
        )); 

var_export(arrayAddChild($data, [ 
         'id' => 33, 
         'uuid' => '33', 
         'parent_uuid' => '32', 
         'name' => 'Second Child', 
         'children' => [] 
         ] 
        )); 
var_export(arrayAddChild($data, [ 
         'id' => 34, 
         'uuid' => '34', 
         'parent_uuid' => '33', 
         'name' => 'Second Child', 
         'children' => [] 
         ] 
        )); 
var_export(arrayAddChild($data, [ 
         'id' => 35, 
         'uuid' => '35', 
         'parent_uuid' => '34', 
         'name' => 'Second Child', 
         'children' => [] 
         ] 
        )); 
var_export(arrayAddChild($data, [ 
         'id' => 36, 
         'uuid' => '36', 
         'parent_uuid' => '35', 
         'name' => 'Second Child', 
         'children' => [] 
         ] 
        )); 


var_export($data); 
+0

非常感謝,奧列格。這對我提供的數據非常有用。但是如果再有一個內在的孩子存在,它就不會將這些孩子加入到這個中。你可以修改,因爲它需要'N'級別的深度? –

+0

這可能會工作: function arrayAddChild(&$ data,$ child){ foreach($ data as $ key => $ value){ if($ value ['uuid'] === $ child ['parent_uuid' ]){ $ data [$ key] ['children'] [] = $ child; 返回true; (arrayAddChild($ data [$ key] ['children'],$ child)){ return true; } } return false; } –

+0

問題是,如果我的項目有兩個孩子,並且如果我嘗試將另一個孩子添加到任何孩子,它顯示「爲foerach suppiied無效的參數」。上述修改不會返回任何錯誤,但不會添加子項。 –