2016-05-17 9 views
3

私はpostgresストアドプロシージャを使用してデータベースからフェッチするタスクIDの配列を持っています。ここに私の配列です:親子関係の配列の印刷

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

MS Project用のXMLファイルを作成できるように目次形式で印刷したいと思います。

この配列は次のようにインデックスを印刷する必要があります:

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

任意の助けを? 私は2日以来立ち往生しています。 おかげ

+0

それは印刷する必要があります1、1.1、1.1.1、...? – Thamilan

+0

タスクが祖父母の場合、それは1を出力し、タスクが直下の子の場合は1.1を出力し、孫の場合は1.1.1を出力します。 –

+0

あなたが尋ねたことは何も分かりません。 –

答えて

0

Online Checkこれはテスト目的のチェックリンクです。

$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/>"; 

結果:

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

ご協力いただきありがとうございます。私はそれを試してみましょう。 –

+0

@ user3445122、あなたの答えは準備ができており、テストしてお知らせします。 –

+0

答えをありがとう。リチャードの答えも良いですが、私は1つしか受け入れることができませんでした。だから私は彼を怒らせた。ありがとう –

0

は、私が仕様を理解しようとしたと私は

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

$両親は、各レベルですべての親を一覧表示され、簡単な解決策を作ってみました。アルゴリズムが既に知っている別の番号があれば、アイテムを追加して$ parent [$ i]の次のインデックスとして使用します。

+0

かなりよく見えます。私はさまざまなシナリオでテストします。それがうまくいくなら、私はそれを受け入れたものとしてマークします。あなたの助けに感謝します。 –

+0

@リチャード、あなたは何をしたのですか? –

+0

はい、私はそれをローカルで実行しました。ここにhttps://3v4l.org/cUA33 – Richard

1

私はあなたがこのために探していると思う:

$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

他の回答をテストするために$関係配列を自由に使用してください:-) –

+0

いいねえ...... –

+0

かなりエレガント! upvoted!ありがとう –