我正在尝试解决这个分支树搜索,我想在1个变量数组中收集分支的所有值,问题是,分支函数当有很多时只返回2个值实际上,我正在使用Codeigniter。
这是我的父功能:
$data['left'] = array();
$data['right'] = array();
$c=array();
foreach ($root as $r){
if ($r->branch_status=="L"){
array_push($data['left'], array($r->member_id, $r->member_name, $r->member_phone));
$c = $this->branch($r->member_id);
for ($i=0; $i<count($c); $i++){
echo "array member : ".$c[$i][0]."<br>";
}
} else if ($r->branch_status="R"){
array_push($data['right'], array($r->member_id, $r->member_name, $r->member_phone));
$c = $this->branch($r->member_id);
for ($i=0; $i<count($c); $i++){
echo "array member : ".$c[$i][0]."<br>";
}
}
}
这是我的分支功能:
public function branch($id){
$br = array();
$branch = $this->M_member->member($id);
foreach ($branch as $b){
if (empty($b)){
echo "empty member";
} else{
array_push($br,array($b->member_id,$b->member_phone));
$this->branch($b->member_id);
}
}
for ($i=0; $i<count($br); $i++){
echo "array member : ".$br[$i][0]." phone : ".$br[$i][1]."<br>";
}
return $br;
}
答案 0 :(得分:1)
主要原因是分支函数中$ br的本地声明。这应该工作
public function branch($id, &$br=array()){
$branch = $this->M_member->member($id);
foreach ($branch as $b){
if (empty($b)){
echo "empty member";
} else{
//array_push($br,array($b->member_id,$b->member_phone));
$br[] = array($b->member_id,$b->member_phone);
$this->branch($b->member_id, $br);
}
}
return $br;
}