2012-04-11 175 views
1

我有一個陣列的動態樹陣列,讓稱之爲$ childrenIds,其輸出如下:創建從另一個陣列

array(

[74252] => Array 
    (
     [0] => 1753 
     [1] => 1757 
     [2] => 1758 
     [3] => 1760 
    ) 

[74238] => Array 
    (
     [0] => 1753 
     [1] => 1755 
     [2] => 1758 
     [3] => 1761 
    ) 

[76476] => Array 
    (
     [0] => 1754 
     [1] => 1755 
     [2] => 1758 
     [3] => 1763 
    ) 

[76478] => Array 
    (
     [0] => 1754 
     [1] => 1756 
     [2] => 1758 
     [3] => 1763 
    ) 

[76480] => Array 
    (
     [0] => 1754 
     [1] => 1757 
     [2] => 1758 
     [3] => 1763 
    ) 

[74253] => Array 
    (
     [0] => 1753 
     [1] => 1757 
     [2] => 1759 
     [3] => 1760 
    ) 

); 我需要做的是從中創建一個新的數組,例如, [74252]將被忽略,但 每個子陣列的孩子徑處理...

所以用這個例子中我的輸出會是這樣的: 陣列(

[1753] => Array 
(
    [1757] => Array 
    (
     [1758] => Array 
     (
      1760 
     ), 
     [1759] => Array 
     (
      1760 
     ), 
    ) 
    [1755] => Array 
     (
      1758 => Array 
      (
       1761 
      ) 
     ) 
    ) 
), 
[1754] => Array 
(
    [1755] => Array 
    (
     [1758] => Array 
     (
      1763 
     ) 
    ), 
    [1756] => Array 
    (
     [1758] => Array 
     (
      1763 
     ) 
    ), 
    [1757] => Array 
    (
     [1758] => Array 
     (
      1763 
     ) 
    ) 
) 
); 

所以不會總是4個子陣列元件,即動態...

父母只是基於該陣列的索引。所以...索引[0]是指數的父[1],索引[1]是index [2]的父母等等。

另外,I想要結束所有的UNIQUE路徑,每個路徑沒有重複的值。

希望我已經清楚地解釋了這一點,一直在尋找幾個小時,找不到滿足我所有要求的解決方案,如果我忽略了一個,我提前道歉。

由於

UPDATE

與之相對傳遞一個數組我結束了通過下劃線分隔的字符串,然後使用此功能:

function explodeTree($array, $delim = '/') 
{ 
$tree = array(); 

foreach($array as $elem) 
{ 
    // Split our string up, and remove any blank items 
    $items = explode($delim, $elem); 
    $items = array_diff($items, array('')); 

    // current holds the current position in the tree 
    $current = &$tree; 

    foreach($items as $item) 
    { 
     // If we've not created this branch before, or there is 
     // a leaf with the same name, then turn it into a branch 
     if(!isset($current[$item]) || !is_array($current[$item])) 
     { 
      $current[$item] = array(); 
     } 

     // Update our current position to the branch we entered 
     // (or created, depending on the above if statement) 
     $current = &$current[$item]; 
    } 

    // If the last value in this row is an array with 0 elements 
    // then (for now) we will consider it a leaf node, and set it 
    // to be equal to the string representation that got us here. 
    if(count($current) == 0) 
    { 
     $current = $elem; 
    } 
} 

return $tree; 
} 

發現@: http://project-2501.net/index.php/2007/10/explodetree/
AND: http://kevin.vanzonneveld.net/techblog/article/convert_anything_to_tree_structures_in_php/

我能夠獲得所需的結果。

+0

如何是每個項目「徑處理」的孩子呢?邏輯是什麼?爲什麼 - 在最終陣列中 - 1753年有1755年和1757年的孩子? (而不是別的) – 2012-04-11 03:44:29

+0

對不起......父母只是基於該數組的索引。所以... index [0]是索引[1]的父親,索引[1]是索引[2]的父親,等等。 – Jim 2012-04-11 03:46:45

+0

所以你需要'(1753-> 1757-> 1758-> 1760,1754-> 1755-> 1758-> 1763,...)''?小心點,以便我們談論的是同樣的事情...... – 2012-04-11 03:48:50

回答

1

對於數組中的第一個元素:

[74252] => Array 
    (
     [0] => 1753 
     [1] => 1757 
     [2] => 1758 
     [3] => 1760 
    ) 

表示的路徑是基本上

[0] => 1753 
[1] => 1753/1757 
[2] => 1753/1757/1758 
[3] => 1753/1757/1758/1760 

你可以可能與這樣的(未測試)解決。 explodeTree函數是從http://kevin.vanzonneveld.net/techblog/article/convert_anything_to_tree_structures_in_php/,我假設它的工作原理與廣告一樣。從來沒有使用過它。

$pathArray = array(); 
foreach($startArray as $subArray) { 
    $pathStr = ''; 

    foreach($subArray as $v) { 
     $pathStr = $pathStr.'/'.$v; 
     $pathArray[]=$pathStr; 
    }   
} 

$pathArray = array_unique($pathArray); 
$treeArray = explodeTree($pathArray, "/"); 
+0

你設置了我的正確路徑...我通過谷歌在這裏找到了一個解決方案... http://project-2501.net/index.php/2007/10/ explodetree /,非常感謝。 – Jim 2012-04-11 04:49:03

0

代碼:(修訂版)

$newarr = array(); 

function getElem($sub,$n) 
{ 
    $res = array(); 

    if ($n==count($sub)-1) 
     $res[]=$sub[$n]; 
    else 
     $res[$sub[$n]] = getElem($sub,$n+1); 

    return $res; 
} 

foreach ($arr as $subarr) 
{ 
    $newarr[$subarr[0]] = getElem($subarr,1); 
} 

print_r($newarr); 

輸入:

$arr= 
array(

74252 => Array 
    (
     0 => 1753, 
     1 => 1757, 
     2 => 1758, 
     3 => 1760 
    ), 

74238 => Array 
    (
     0 => 1753, 
     1 => 1755, 
     2 => 1758, 
     3 => 1761 
    ), 

76476 => Array 
    (
     0 => 1754, 
     1 => 1755, 
     2 => 1758, 
     3 => 1763 
    ), 

76478 => Array 
    (
     0 => 1754, 
     1 => 1756, 
     2 => 1758, 
     3 => 1763 
    ) 
); 

輸出:

Array 
(
    [1753] => Array 
     (
      [1755] => Array 
       (
        [1758] => Array 
         (
          [0] => 1761 
         ) 

       ) 

     ) 

    [1754] => Array 
     (
      [1756] => Array 
       (
        [1758] => Array 
         (
          [0] => 1763 
         ) 

       ) 

     ) 

) 
+0

那麼這就是所需的輸出!然而,我的主陣列中有5個元素與我分享的內容不同,子元素的數量也會有所不同。我欣賞儘管。 – Jim 2012-04-11 04:10:20

+0

@Jim給我一下,我也會讓它處理可變的子元素。 – 2012-04-11 04:11:19

+0

@Jim我剛剛更新了我的答案......它現在可以用於主陣列中的任何元素,以及任何數量的子元素... :-) – 2012-04-11 04:20:04