Php 使用函数时如何获取实际变量而不是副本
正如标题所示,我有一个函数,我对数组做了一些更改(这个数组是我的参数)。然后我意识到我使用了一个实际数组的副本 我知道有一种方法可以得到实际的数组而不是副本,那是什么? 提前谢谢大家,我知道你们会很快解决这个问题:) 这是我使用它的地方Php 使用函数时如何获取实际变量而不是副本,php,Php,正如标题所示,我有一个函数,我对数组做了一些更改(这个数组是我的参数)。然后我意识到我使用了一个实际数组的副本 我知道有一种方法可以得到实际的数组而不是副本,那是什么? 提前谢谢大家,我知道你们会很快解决这个问题:) 这是我使用它的地方 function findChildren($listOfParents) { static $depth=-1; $depth++; foreach ($listOfParents as $thisPa
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,而不是他的副本。尝试通过引用传递值
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--;
}
请注意符号“
”和“
”,它表示您正在处理原始变量,而不是副本。尝试通过引用传递值
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--;
}
请注意符号
&
,它表示您正在处理原始变量,而不是副本。您所说的是通过引用传递变量:
试试这个:
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--;
}
您所说的是通过引用传递变量: 试试这个:
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--;
}