我有一個陣列的動態樹陣列,讓稱之爲$ 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/
我能夠獲得所需的結果。
如何是每個項目「徑處理」的孩子呢?邏輯是什麼?爲什麼 - 在最終陣列中 - 1753年有1755年和1757年的孩子? (而不是別的) – 2012-04-11 03:44:29
對不起......父母只是基於該數組的索引。所以... index [0]是索引[1]的父親,索引[1]是索引[2]的父親,等等。 – Jim 2012-04-11 03:46:45
所以你需要'(1753-> 1757-> 1758-> 1760,1754-> 1755-> 1758-> 1763,...)''?小心點,以便我們談論的是同樣的事情...... – 2012-04-11 03:48:50