ich ein Array habe, läßt es $ childrenIds nennt, gibt sie wie folgt:einen dynamischen Baum Array von einem anderen Array erstellen
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
)
); Was ich tun muss, ist ein neues Array von diesem zu erstellen, wo die z. [74252] ignoriert wird, aber die Kinder jedes Subarray sind pathed ...
So dieses Beispiel mit meiner Ausgabe etwas aussehen würde: 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
)
)
)
);
So wird es nicht immer 4 Sub-Array-Elemente, das heißt dynamisch ...
Die Eltern basieren nur auf dem Index dieses Arrays, also ... index [0] ist das übergeordnete Element von index [1], index [1] ist das Eltern von Index [2] usw.
Auch ich Sie möchten alle eindeutigen Pfade erhalten, keine doppelten Werte pro Pfad.
Hoffentlich habe ich das klar erklärt, bin seit ein paar Stunden gesucht und kann keine Lösung finden, die alle meine Anforderungen erfüllt, wenn ich einen übersehen habe, entschuldige ich mich im Voraus.
Dank
UPDATE
Hinsichtlich Leiten eines Array Gegensatz I einen Unterstrich getrennte Zeichenfolge am Ende vorbei, dann ist diese Funktion:
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;
}
gefunden @: http://project-2501.net/index.php/2007/10/explodetree/
UND: http://kevin.vanzonneveld.net/techblog/article/convert_anything_to_tree_structures_in_php/
Ich konnte das gewünschte Ergebnis erzielen.
Wie werden die Kinder jedes Elements "patient"? Was ist die Logik? Warum - 1753 1753 und 1757 als Kinder? (und nicht etwas anderes) –
Sorry ... die Eltern basieren nur auf dem Index dieses Arrays. Also ... index [0] ist das übergeordnete Element von Index [1], index [1] ist das übergeordnete Element von index [2] und so weiter. – Jim
Also brauchst du so etwas wie '(1753-> 1757-> 1758-> 1760, 1754-> 1755-> 1758-> 1763, ...)'? Seien Sie vorsichtig, so dass wir über die gleiche Sache reden .... –