2012-12-19 150 views
4

我在設計評論系統,但在我的遞歸函數中有一些問題。如果在這個代碼看起來如下:問題在我的遞歸函數

$list = array(
      array('id'=>1,'parent'=>0), 
      array('id'=>2,'parent'=>1), 
      array('id'=>3,'parent'=>0), 
      array('id'=>4,'parent'=>0), 
      array('id'=>5,'parent'=>4), 
      array('id'=>6,'parent'=>4) 
     ); 

$c = count($list); 

$comment = array(); 

function setParentStyleComment($cmnt){ 
    return 'id: '.$cmnt['id'].' - parent: '.$cmnt['parent'].' - [PARENT]'; 
} 

function setReplyStyleComment($cmnt){ 
    return 'id: '.$cmnt['id'].' - parent: '.$cmnt['parent'].' - [REPLY]'; 
} 

function getComment($p) { 
    global $comment,$list,$c; 
    foreach($list as $L){ 
     if(($L['parent'] == 0 || $L['parent'] != $p) && $L['id'] != $p) 
     { 
      $comment[] = setParentStyleComment($L); 
      $x = $L['id']; 
      array_shift($list); 
      getComment($x); 
     } 
     else if($L['id'] != $p) 
     { 
      $comment[] = setReplyStyleComment($L); 
      $x = $L['id']; 
      array_shift($list); 
      if($x < $c){ 
       getComment($x); 
      } 
     } 
    } 
} 

getComment(0); 
echo "<pre>"; 
print_r($comment); 
echo "</pre>\n<br/>"; 

上述代碼中,有這樣的結果:

Array 
(
[0] => id: 1 - parent: 0 - [PARENT] 
[1] => id: 2 - parent: 1 - [REPLY] 
[2] => id: 3 - parent: 0 - [PARENT] 
[3] => id: 4 - parent: 0 - [PARENT] 
[4] => id: 5 - parent: 4 - [REPLY] 
[5] => id: 6 - parent: 4 - [PARENT] 
) 

但它必須有這樣的結果:

Array 
(
[0] => id: 1 - parent: 0 - [PARENT] 
[1] => id: 2 - parent: 1 - [REPLY] 
[2] => id: 3 - parent: 0 - [PARENT] 
[3] => id: 4 - parent: 0 - [PARENT] 
[4] => id: 5 - parent: 4 - [REPLY] 
[5] => id: 6 - parent: 4 - [REPLY] 
) 

我怎樣才能解決這個功能問題?

回答

2

遞歸?

$list = array(
    array('id' => 1, 'parent' => 0), 
    array('id' => 2, 'parent' => 1), 
    array('id' => 3, 'parent' => 0), 
    array('id' => 4, 'parent' => 0), 
    array('id' => 5, 'parent' => 4), 
    array('id' => 6, 'parent' => 4) 
); 

$comments = array_map(function($item) { 
    return sprintf(
     'id: %d - parent: %d - [%s]', 
     $item['id'], $item['parent'], $item['parent'] ? 'REPLY' : 'PARENT'); 
}, $list); 

echo "<pre>"; 
print_r($comments); 
echo "</pre>\n<br/>"; 

作爲(完整)的例子所示,這是一個簡單array_map操作。

+1

+1尼斯測繪技能... – Baba

+0

是的,需要刪除虛假功能:) * gg * – hakre

+0

我無法想象所有那些長代碼只是爲了一個簡單的任務 – Baba

6

我覺得你是一個簡單的任務複雜....一個簡單的循環就足夠了

$final = array(); 
foreach ($list as $value) { 
    $final[] = !$value['parent'] 
       ? setParentStyleComment($value) 
       : setReplyStyleComment($value); 
} 

print_r($final); 

輸出

Array 
(
     [0] => id: 1 - parent: 0 - [PARENT] 
     [1] => id: 2 - parent: 1 - [REPLY] 
     [2] => id: 3 - parent: 0 - [PARENT] 
     [3] => id: 4 - parent: 0 - [PARENT] 
     [4] => id: 5 - parent: 4 - [REPLY] 
     [5] => id: 6 - parent: 4 - [REPLY] 
) 

See Live Demo

+1

感謝這個答案,我真的想知道這個決定是什麼,以及它更微不足道;)http://stackoverflow.com/a/13962565/367456 – hakre

+0

@Baba,你的代碼不適用於任何輸入。如果輸入是這樣的話會發生什麼: '$ list = array(array('id'=> 1,'parent'=> 0),array('id'=> 2,'parent'=> 1),陣列( 'ID'=> 3 '父'=> 0),陣列( 'ID'=> 4, '父'=> 0),陣列( 'ID'=> 5, '父'=> 4) ,數組('id'=> 6,'pa​​rent'=> 0),array('id'=> 7,'parent'=> 4));' – return

+0

@Baba,以上輸入的結果是: '數組 ( [0] => id:1 - parent:0 - [PARENT] [1] => id:2 - parent:1 - [REPLY] [2] => id:3 - parent:0 - [父母] [3] => id:4 - 父母:0 - [父母] [4] => id:5 - parent:4 - [REPLY] [5] => id:6 - parent: 0 - [PARENT] [6] => id:7 - parent:4 - [REPLY] )' – return