2014-10-01 53 views
-1

我有一個分層數據,我把它放在一個數組中,我叫$ dt。比起我有一個數組來存儲我調用$ in的這些數據之間的關係。我創建了一個函數,它具有參數,初始索引,級別,數組$ in和數組$ in。我正在調試該函數,但我不知道爲什麼該子類在此過程中丟失。完整的代碼是:遞歸PHP函數返回意外的數組

$dt = array(
    41=>array("pk"=>41,"parentPk"=>30,"name"=>"car1"), 
    15=>array("pk"=>15,"parentPk"=>11,"name"=>"food"), 
    70=>array("pk"=>70,"parentPk"=>30,"name"=>"car3"), 
    18=>array("pk"=>18,"parentPk"=>15,"name"=>"food1"), 
    49=>array("pk"=>49,"parentPk"=>30,"name"=>"car2"), 
    20=>array("pk"=>20,"parentPk"=>15,"name"=>"food2"), 
    30=>array("pk"=>30,"parentPk"=>11,"name"=>"car")  
); 
echo "<pre>"; 
print_r($dt); 
echo "</pre>"; 
$in=array(11=>array(15,30),15=>array(18,20),30=>array(41,49,70)); 
echo "<pre>"; 
print_r($in); 
echo "</pre>"; 

function fn_tree($parent_id, $level,$dt,$in) { 

if(is_null($parent_id)){ 
     $parent_id ="NULL"; 
    } 

    if (isset($in[$parent_id])) {     

     foreach ($in[$parent_id] as $id) { 

      $pk=$in[$parent_id]; 

      $arrEnd[$id]=str_repeat("-", $level*2) .$dt[$id]["name"]; 

      fn_tree($id, $level + 1,$dt,$in,$arrEnd); 

     }   
}  
return $arrEnd; 
} 


$arrEcho = fn_tree(11, 0, $dt, $in); 

echo "<br>"; 
echo "result"; 
echo "<br>"; 
echo "<pre>"; 
print_r($arrEcho); 
echo "</pre>"; 
$desired = array(
    15=>"food", 
    18=>"food1", 
    20=>"food2", 
    30=>"car", 
    41=>"car1", 
    49=>"car2", 
    70=>"car3" 
); 

echo "<br>"; 
echo "desired"; 
echo "<br>"; 
echo "<pre>"; 
print_r($desired); 
echo "</pre>"; 

那麼,爲什麼這個函數正在丟失子類呢?

輸出是:

Array data 
(
[41] => Array 
    (
     [pk] => 41 
     [parentPk] => 30 
     [name] => car1 
    ) 

[15] => Array 
    (
     [pk] => 15 
     [parentPk] => 11 
     [name] => food 
    ) 

[70] => Array 
    (
     [pk] => 70 
     [parentPk] => 30 
     [name] => car3 
    ) 

[18] => Array 
    (
     [pk] => 18 
     [parentPk] => 15 
     [name] => food1 
    ) 

[49] => Array 
    (
     [pk] => 49 
     [parentPk] => 30 
     [name] => car2 
    ) 

[20] => Array 
    (
     [pk] => 20 
     [parentPk] => 15 
     [name] => food2 
    ) 

[30] => Array 
    (
     [pk] => 30 
     [parentPk] => 11 
     [name] => car 
    ) 

) 
Array relationship 
(
[11] => Array 
    (
     [0] => 15 
     [1] => 30 
    ) 

[15] => Array 
    (
     [0] => 18 
     [1] => 20 
    ) 

[30] => Array 
    (
     [0] => 41 
     [1] => 49 
     [2] => 70 
    ) 

) 

result 
Array 
(
[15] => food 
[30] => car 
) 

desired 
Array 
(
[15] => food 
[18] => food1 
[20] => food2 
[30] => car 
[41] => car1 
[49] => car2 
[70] => car3 
) 

回答

1

的線索,你的問題是/應該是 「未定義的變量:arrEnd。」如果您沒有在可以看到此警告的環境中運行,則應嘗試設置此類環境。如果你在這樣的環境中跑步,你應該密切關注這個和所有其他警告。

我不確定這是否只是給出線索或整體答案,而是繼續給出一個相當完整的答案:看起來你打算通過引用傳遞arrEnd,但沒有通過它在所有。

此外,我有點困惑你的期望輸出。難道你不是真的希望用雙破折號展示層次結構嗎?

Array 
(
    [15] => food 
    [18] => --food1 
    [20] => --food2 
    [30] => car 
    [41] => --car1 
    [49] => --car2 
    [70] => --car3 
) 
+1

嗨bfd,我試過聲明之前的功能,但這不起作用。是的,它應該爲每個子目錄提供2行。 – zwitterion 2014-10-02 00:08:09

+1

事實上,如果我在函數內聲明'global $ dt,$ in,$ arrEnd;',它會起作用,我不知道爲什麼。 – zwitterion 2014-10-02 00:18:49