Php 值不会动态添加到数组中
在CodeIgniter中,我想从两个不同的函数向数组中添加值,但这些值只在第一个函数中添加到数组中。请告诉我可能是什么问题 代码:Php 值不会动态添加到数组中,php,arrays,codeigniter,Php,Arrays,Codeigniter,在CodeIgniter中,我想从两个不同的函数向数组中添加值,但这些值只在第一个函数中添加到数组中。请告诉我可能是什么问题 代码: public $ChnCat_tags = array(); function first_function() { //some code $ChnCat_tags[] = array( 'level' => $level, 'value' => $row_vct->id_vct,
public $ChnCat_tags = array();
function first_function() {
//some code
$ChnCat_tags[] = array(
'level' => $level,
'value' => $row_vct->id_vct,
'label' => $row_vct->displayname_vct,
'disable' => $disb
);
$recursion_result = second_function($ChnCat_tags);
return $ChnCat_tags; //only returns values added inside first_function
}
function second_function($ChnCat_tags) {
//some code
$ChnCat_tags[] = array(
'level' => $level,
'value' => $row_vct->id_vct,
'label' => $row_vct->displayname_vct,
'disable' => $disb
);
recursion_result = second_function($ChnCat_tags);
return recursion_result;
}
你可以开始使用
$this->ChnCat_标签
,而不是到处使用ChnCat_标签
或():
你喜欢这样吗
function first_function() {
$ChnCat_tags[] = array(
'level' => $level,
'value' => $row_vct->id_vct,
'label' => $row_vct->displayname_vct,
'disable' => $disb
);
# Must call function with $this
$recursion_result = $this->second_function($ChnCat_tags);
# print the value of $recursion_result whish hold entire data
print_r($recursion_result);
}
function second_function($ChnCat_tags) {
$ChnCat_tags[] = array(
'level' => $level,
'value' => $row_vct->id_vct,
'label' => $row_vct->displayname_vct,
'disable' => $disb
);
# just return the array data.
return $ChnCat_tags;
}
您的代码无法编译,请重新编译:
递归\u结果
。。。所以这可能是你的主要问题。。。这是怎么运行的?@Alex,现在看。它还没有完全修复,但我明白了。@Alex,你能回答吗?我有点困惑。。。在第二个函数中,您再次运行自身(在自身内部),这将导致无休止的循环。这是一个错误吗?
function first_function() {
$ChnCat_tags[] = array(
'level' => $level,
'value' => $row_vct->id_vct,
'label' => $row_vct->displayname_vct,
'disable' => $disb
);
# Must call function with $this
$recursion_result = $this->second_function($ChnCat_tags);
# print the value of $recursion_result whish hold entire data
print_r($recursion_result);
}
function second_function($ChnCat_tags) {
$ChnCat_tags[] = array(
'level' => $level,
'value' => $row_vct->id_vct,
'label' => $row_vct->displayname_vct,
'disable' => $disb
);
# just return the array data.
return $ChnCat_tags;
}