2012-05-11 20 views
0

由於標題摘要,我有一個函數,我對數組做了一些更改(這個數組是我的參數)。然後我意識到我使用了我的實際數組的副本。如何獲得實際變量,而不是使用函數時的副本

我知道有一種方法來獲得實際的數組而不是副本,它是什麼? 謝謝大家提前,我知道你會在一個瞬間:)解決這個

這裏是我用它

function findChildren($listOfParents) 
    { 
     static $depth=-1; 
     $depth++; 

     foreach ($listOfParents as $thisParent) 
     { 
      $thisParent->title = str_repeat(" >", $depth) . $thisParent->title; 
      $children = page::model()->findAll(array('condition'=>'parent = ' . $thisParent->id)); 
      findChildren($children); 
     } 

     $depth--; 
    } 

所以我需要這個$ listOfParents,不是他的副本。

回答

3

嘗試通過引用傳遞值

function findChildren(&$listOfParents) 
    { 
     static $depth=-1; 
     $depth++; 

     foreach ($listOfParents as $thisParent) 
     { 
      $thisParent->title = str_repeat(" >", $depth) . $thisParent->title; 
      $children = page::model()->findAll(array('condition'=>'parent = ' . $thisParent->id)); 
      findChildren($children); 
     } 

     $depth--; 
    } 

通知的符號&,這表明你正在使用的原變量,而不是一個副本。

+0

謝謝,它幫助:) – Jordashiro

+0

不要忘記接受答案,當你可以:) – freshnode

1

你說的是按引用傳遞變量:http://php.net/manual/en/language.references.pass.php

試試這個:

function findChildren(&$listOfParents) 
    { 
     static $depth=-1; 
     $depth++; 

     foreach ($listOfParents as $thisParent) 
     { 
      $thisParent->title = str_repeat(" >", $depth) . $thisParent->title; 
      $children = page::model()->findAll(array('condition'=>'parent = ' . $thisParent->id)); 
      findChildren($children); 
     } 

     $depth--; 
    } 
相關問題