2017-05-31 211 views
1

我有以下示例數組列表;列表樹結構

$relationships = ['contact', 'property', 'contact.type1', 'contact.type2', 'contact.type1.color1', 'contact.type1.color2']; 

我想要一個這樣的結構;

[ 
    contact => [ 
       type1 => [ 
          color1 => [], 
          color2 => [] 
          ], 
       type2 => [] 
       ], 
    property => [] 
] 

我創建了下面的代碼;

start(); 

function start() 
{ 
    $myTree = array(); 

    $relations = ['contact', 'property', 'contact.type1', 'contact.type2', 'contact.type1.color1', 'contact.type1.color2']; 

    foreach ($relations as $relation) { 

     $myTree = getRelationTree($relation, $myTree); 
    } 

    var_dump($myTree); 
} 

function getRelationTree($name, $tree) 
{ 
    $sections = explode('.', $name); 

    $main = array_shift($sections); 

    if (!array_search($main, $tree)) { 
     $tree[$main] = [] ; 
    } 

    // Has nested relationships 
    if (count($sections) > 0) { 
     $children = []; 
     $children[] = getRelationTree(join('.',$sections), $children); 

     $tree[$main] = $children; 
    } 

    return $tree; 
} 

不過,我得到的結果是缺少color1按照上述預期的結果。

你能告訴我我的代碼出了什麼問題,以及如何修復它以給我想要的結果?謝謝

+0

我真的被它的邏輯要變換數組到所需的格式排列不明白?請告訴我? –

+0

這是一個業務需求。我在這裏給出的例子只是我將要接受的結構樣本和我需要輸出的結構。 –

+0

如果沒有邏輯背後,那麼爲什麼你不手動hardcord該類型的數組 –

回答

2

我剛剛寫了簡單的代碼使用引用,而不是recurency。

看看我的代碼,它包含註釋說發生了什麼:

<?php 

$relationships = ['contact', 'property', 'contact.type1', 'contact.type2', 'contact.type1.color1', 'contact.type1.color2']; 

$tree = []; 
foreach($relationships as $path) { 
    $parts = explode('.', $path); 

    //represents current position in current $path 
    $currentNode = &$tree; 

    foreach($parts as $part) { 
     //if this part doesn't exist yet, let's create it 
     if(empty($currentNode[$part])) { 
      $currentNode[$part] = []; 
     } 
     //move forward in out $path position 
     $currentNode = &$currentNode[$part]; 
    } 
    //after we finish a particular path, we reset the position. 
    unset($currentNode); 
} 

工作例如:https://3v4l.org/o3MGP

編輯:這是你的錯誤解釋:

正如我已經分析了您的代碼,我可以在你的功能getRelationTree()這裏看到你的錯誤:

// Has nested relationships 
if (count($sections) > 0) { 
    $children = []; 
    $children[] = getRelationTree(implode('.',$sections), $children); 

    $tree[$main] = $children; // <==this line exactly 
} 

您正在覆蓋已有的值。

所以,當你的樹已經解析了'contact.type1.color1'路徑,而你解析'contact.type1.color2',將覆蓋$tree['contact']已經有與type1.color2新樹從'type1.color1'值。等等。

+0

@MokkyMiah確認看到我的更新。 –

0

這裏是另一個答案的替代,使用遞歸(複製粘貼準備):

$relationships = ['contact', 'property', 'contact.type1', 'contact.type2', 'contact.type1.color1', 'contact.type1.color2']; 

$arr = array(); 

foreach($relationships as $value){ 
    add($arr, $value); 
} 

function add(&$arr, $value){ 
    if(strpos($value, '.') !== false){ // if there are sublevels 
     $tmp = explode('.', $value, 2); // separate first from rest of levels 
     $item = $tmp[0]; // get what's before first dot 
     $children = $tmp[1]; // get what's after the first dot 
     if(empty($arr[$item])){ // if not yet created, create item 
      $arr[$item] = array(); 
     } 
     add($arr[$item], $children); // add the item's child/children 
     return; // prevent from overriding 
    } 
    $arr[$value] = array(); 
} 

echo '<pre>'; 
print_r($arr);