2012-04-11 12 views
1

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.

+0

Wie werden die Kinder jedes Elements "patient"? Was ist die Logik? Warum - 1753 1753 und 1757 als Kinder? (und nicht etwas anderes) –

+0

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

+0

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 .... –

Antwort

1

Für das erste Element im Array:

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

Die Wege dargestellt sind grundsätzlich

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

Sie wahrscheinlich mit so etwas wie dieses (nicht getestet) lösen könnte. Die explodeTree Funktion ist von http://kevin.vanzonneveld.net/techblog/article/convert_anything_to_tree_structures_in_php/ und ich gehe davon aus, dass es wie angekündigt funktioniert. Habe es nie selbst benutzt.

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

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

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

Du hast mich auf den richtigen Weg gesetzt ... Ich habe über google eine Lösung gefunden ... http://project-2501.net/index.php/2007/10/ explodetree /, vielen Dank. – Jim

0

Code:(Deutsch)

$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); 

Input:

$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 
    ) 
); 

Output:

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

       ) 

     ) 

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

       ) 

     ) 

) 
+0

Nun, das ist die gewünschte Ausgabe! Es gibt jedoch viel mehr als die 5 Elemente meines Hauptarrays, die ich mit Ihnen geteilt habe, und die Anzahl der Unterelemente variiert ebenfalls. Ich schätze die Mühe jedoch. – Jim

+0

@Jim Gib mir eine Sekunde, und ich werde dafür sorgen, dass auch variable Unterelemente behandelt werden. –

+0

@Jim Ich habe gerade meine Antwort aktualisiert ... es funktioniert jetzt für ALLE Elemente im Haupt-Array und für JEDE Anzahl von Unterelementen ... :-) –

Verwandte Themen