2016-05-17 7 views
3

Ich habe ein Array von Task-IDs, die ich aus der Datenbank mit Postgres gespeicherten Prozedur abrufen. Hier ist meine Array:Drucken Array in Eltern-Kind-Beziehung

$relationships=array(
      array(70),//This represents calculated hierarchy field in a record 
      array(70, 71),//This represents calculated hierarchy field in a record 
      array(70, 71, 72),//This represents calculated hierarchy field in a record 
      array(70, 71, 72, 68)//This represents calculated hierarchy field in a record 
     ); 

Ich möchte, dass sie in Inhaltsverzeichnis Format drucken zu können XML-Datei für MS Project erstellen.

sollte Dieses Array-Indizes wie folgt drucken:

$relationships=array(
      array(70),//Should print 1 because its grandparent task 
      array(70, 71),//Should print 1.1 because its child of task 70 
      array(70, 71, 72),//Should print 1.1.1 because its child of task 71 
      array(70, 71, 72, 68)//Should print 1.1.1.1 
     ); 

Jede Hilfe? Ich bin seit zwei Tagen festgefahren. Dank

+0

Wenn es 1 gedruckt werden soll, 1.1, 1.1.1, ...? – Thamilan

+0

Wenn die Aufgabe Großelternteil ist, wird 1 gedruckt, wenn die Aufgabe unmittelbar untergeordnet ist, wird 1.1 gedruckt, und wenn die Aufgabe Enkelkind ist, sollte sie 1.1.1 drucken. –

+0

Ich verstehe nichts von dem, was Sie gefragt haben. –

Antwort

0

Online Check, Dies ist der Prüfling für Testzwecke .

$relationships=array(
      array(70), 
      array(70, 71), 
      array(70, 71, 72), 
      array(70, 71, 72, 68), 
      array(80) 
     ); 
$old_count = 0; 
$index = 0; 
foreach($relationships as $val){  
    $tmp = array(); 
    $value = array(); 
    $count = count($val); 
    if($count == 1){ 
     $old_count = 0; 
     $level = array();  
     $index++; 
     $level[] = $index; 
    } 

    if($old_count == $count) 
     $level[($count-1)] = ($level[($count-1)] == "" || $level[($count-1)] == null) ? 1 : ++$level[($count-1)]; 
    else if($old_count > $count){ 
     $level[($count-1)] = ($level[($count-1)] == "" || $level[($count-1)] == null) ? 1 : ++$level[($count-1)]; 
     for($j = $count; $j <= $old_count; $j++) 
      $level[$j] = 1; 
    } 
    else 
     $level[$count] = 1; 

    for($i = 0; $i < $count; $i++){ 
     $tmp[] = $level[$i]; 
    } 
    $old_count = $count; 
    echo "Indexes: ".implode(".", $tmp)."<br/>"; 

Ergebnis:

Indexes: 1 
Indexes: 1.1 
Indexes: 1.1.1 
Indexes: 1.1.2 
Indexes: 1.2 
Indexes: 2 
Indexes: 2.1 
Indexes: 2.1.1 
Indexes: 2.1.2 
Indexes: 2.1.2.1 
+0

Vielen Dank für Ihre Hilfe. Lass mich das versuchen. –

+0

@ user3445122, Ihre Antwort ist bereit, Test und lass es mich wissen. –

+0

Danke für die Antwort. Richards Antwort ist auch gut, aber ich könnte nur eine annehmen. Also habe ich ihn aufgewertet. Danke –

0

Ich habe versucht, die Spezifikation zu verstehen, und ich habe mit einfacher Lösung

$relationships=array(
    array(70),//Should print 1 because its grandparent task 
    array(70, 71),//Should print 1.1 because its child of task 70 
    array(70, 71, 72),//Should print 1.1.1 because its child of task 71 
    array(70, 71, 72, 68),//Should print 1.1.1.1 
    array(70, 71, 72, 69),//Should print 1.1.1.2 
    array(70, 73), //Should print 1.2 

); 

$parents = array(); 

foreach($relationships as $row) { 
    // parsing row 
    $row_output = array(); 
    for($i = 0; $i < count($row); $i++) 
    { 

     if(!isset($parents[$i])) 
      $parents[$i] = array(); 

     $index = array_search($row[$i], $parents[$i]); 
     if($index !== FALSE) { 
      $row_output[] = $index+1; 
     } 
     else { 
      $index = count($parents[$i]); 
      $parents[$i][] = $row[$i]; 
      $row_output[] = $index+1; 
     } 
    } 
    echo implode('.', $row_output) . "\n"; 
} 

// var_export($parents); 

die $ Eltern all Eltern listen auf jeder Ebene kommen. Wenn also eine andere Nummer existiert, die der Algorithmus bereits kennt, fügt er ein Element hinzu und verwendet es als nächsten Index (basierend auf bereits vorhandenen Elementen) in $ parent [$ i]

+0

Sieht ganz gut aus .. Ich werde in verschiedenen Szenarien testen. Wenn es gut funktioniert, dann werde ich es als akzeptiert markieren. Vielen dank für Deine Hilfe. –

+0

@Richard, Hast du überprüft, was du getan hast ??? –

+0

Ja, ich habe es lokal ausgeführt. und hier https://3v4l.org/cUA33 – Richard

1

Ich glaube, Sie suchen dafür:

$relationships = array(
    array(70, 71, 72),  // 1.1.1 
    array(80),    // 2 
    array(75, 71, 72),  // 3.1.1 
    array(80, 72),   // 2.1 
    array(75, 72, 72),  // 3.2.1 
    array(70),    // 1 
    array(70, 71, 74),  // 1.1.2 
    array(80, 71, 1, 2, 3, 4, 5, 6, 7, 8, 9),   // 2.2.1.1.1.1.1.1.1.1.1 
    array(80, 71, 1, 2, 3, 4, 5, 6, 7, 8, 0)   // 2.2.1.1.1.1.1.1.1.1.2 
); 

function find_all($values, &$arr){ 
    if(count($values) == 0){ 
     echo "\n"; return; 
    } 
    if(array_key_exists($values[0], $arr)) 
     echo (array_search($values[0], array_keys($arr))+1).'.'; 
    else { 
     echo (count($arr)+1).'.'; 
     $arr[$values[0]] = array(); 
    } 
    find_all(array_slice($values, 1), $arr[$values[0]]); 
} 

$storage = array(); 
foreach($relationships as $array) 
    find_all($array, $storage); 
+0

Fühlen Sie sich frei, meine $ Beziehungen Array zu verwenden, um andere Antworten zu testen :-) –

+0

Sieht gut aus ...... –

+0

Ziemlich elegent! aufgewertet! Vielen Dank –