私は配列を持っています、それを$ 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]は無視されますが、各サブ配列の子はパスされます...
したがって、この例を使用すると、出力は次のようになります。array(
[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つのサブ配列要素が常に存在するとは限りません...
親は、その配列のインデックスに基づいています。つまり...index[0]はindex[1]の親であり、index[1]はindex[2]の親です。
また、パスごとに重複する値がないように、すべてのUNIQUEパスで終了したいと思います。
うまくいけば、私はこれを明確に説明し、数時間を探していて、私の要件をすべて満たす解決策を見つけることができません。見落とした場合は、事前に謝罪します。
ありがとう
アップデート
配列を渡すのとは対照的に、アンダースコアで区切られた文字列を渡すことになり、次の関数を使用します。
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/
思い通りの結果が得られました。