2016-03-22 42 views
1

如何創建一個將數組顯示爲樹的函數。例如,我想獲得一棵我想走的決策樹,直到根據分支的值得到葉子。我創建樹像波紋管:如何創建一個函數來顯示一個數組作爲一棵樹?

$tree= new DS_Tree(); 
$node=array('name' => 'start'); 

$tree->insert_node($node); 
$tree->goto_root(); 

    $mytree = new id3(); 
    $mytree->init($data_array_AttrList,$data_array_values,$data_class,$data_array_instances,$tree); 
    $mytree->run(); 
    echo '<pre class="brush: php">'; 
     print_r($mytree->tree->draw_tree()); 
    echo '</pre>'; 

功能draw_tree()是:

public function draw_tree() { 
    return $this->nodes; 
} 

創建我的樹的功能是:

private function make_tree($attr) { 
    foreach($this->Values[$attr] as $v) { 
     $subset = $this->get_subset($attr, $v); 
     if($the_class = $this->has_same_class($subset)) { 
      $node =array(
       'name' => $attr, 
       'arc' => $v 

      ); 
      $this->tree->insert_node($node); 

      $this->Instance = array_diff_key($this->Instance, $subset); 
     } else { 
      $node =array(
       'name' => $this->Classa, 
       'arc' => $v 
      ); 

      $unresolved = $this->tree->insert_node($node); 
     } 
    } 

    if (isset($unresolved)) { 

     $this->tree->goto_index($unresolved); 

    } 

    } 
} 

結果是:

Array 
(
[0] => Array 
    (
     [name] => Time 
     [parent] => 
     [children] => Array 
      (
       [0] => 1 
      ) 

    ) 

[1] => Array 
    (
     [name] => Focus 
     [arc] => Array 
      (
       [0] => 2 day/week 
       [1] => 3 day/week 
       [2] => 4 day/week 
       [3] => 5 day/week 
       [4] => 6 day/week 
      ) 

     [parent] => 0 
     [children] => Array 
      (
       [0] => 2 
      ) 

    ) 

[2] => Array 
    (
     [name] => Dificulty 
     [arc] => Array 
      (
       [0] => Weght loss 
       [1] => Mantain weight 
       [2] => Gain Mass 
      ) 

     [parent] => 1 
     [children] => Array 
      (
       [0] => 3 
      ) 

    ) 

[3] => Array 
    (
     [name] => Sex 
     [arc] => Array 
      (
       [0] => Beginner 
       [1] => Intermediar 
       [2] => Advance 
      ) 

     [parent] => 2 
     [children] => Array 
      (
       [0] => 4 
      ) 

    ) 

[4] => Array 
    (
     [name] => Array 
      (
       [Exercise] => Array 
        (
         [0] => Array 
          (
           [0] => Ex1 
           [1] => Ex2 
           [2] => Ex3 
           [3] => Ex4 
           [4] => Ex5 

          ) 

        ) 

      ) 

     [arc] => Array 
      (
       [0] => F 
       [1] => M 
      ) 

     [parent] => 3 
    ) 

) 

回答

1

只顯示一個數組作爲樹:

echo "<pre>"; 
var_dump($array); 
echo "</pre>"; 
0

這裏是通過這個數據結構進行迭代,尋找某一特定值的方式:

function recurseFind($tree, $find, $path = "") { 
    if (!is_array($tree)) { 
     // it is a leaf: 
     if ($tree === $find) { 
      return $path; // return path where we found it 
     } 
     return false; 
    } 
    foreach($tree as $key => $value) { 
     $result = recurseFind($value, $find, $path . (is_numeric($key) ? "[$key]" : "['$key']")); 
     if ($result !== false) return $result; 
    } 
    return false; 
} 

對於樣本輸入你提供的,如果你想這樣稱呼它:

echo recurseFind($tree, "Mantain weight", '$tree'); 

輸出的格式值(第一匹配只),其可以在PHP進行評價的「位置」:

$tree[2]['arc'][1] 
+0

我該如何走這棵樹?因爲我想將其與來自用戶的輸入數據進行比較 – Ciro

+0

您是否可以更新您的問題並舉例說明此類輸入以及您希望從中獲得哪些輸出? – trincot

+0

如果用戶引入這樣的輸入,例如:時間= 2天/周,焦點=保持體重並且難度=前進....則輸出= Ex2(例如) – Ciro

相關問題