search

Home  >  Q&A  >  body text

PHP recursion problem, please help

已经有数据
id  fid
23  0
27  23
28  23
34  38
33  0

//getID 是从数据库取出来的,目前 23 和 33 
foreach ($getID as $k => $v) {
    $allAgentID = getAllID($v);
    print_r($allAgentID);
}

function getAllID($uid){
	global $empire,$dbtbpre,$userid;
	$userid[] =$uid ;
	$s= "select userid from {$dbtbpre}enewsmember where fid='".$uid."' and checked=1 order by userid asc";
	$sql=$empire->query($s);
	while($u=$empire->fetch($sql)) {
	    getAllID($u[userid]);
	}
	return $userid;
}

//输出结果
Array    
(    
[0] => 23    
[1] => 27    
[2] => 28    
[3] => 34    
)    
Array    
(    
[0] => 23    
[1] => 27    
[2] => 28    
[3] => 34    
[4] => 33    
)      

为什么当$v=33的时候没有清楚之前的数组,还把之前的数组合并在一起

请问有什么办法能得到以下数组吗?
当$v=23时
Array    
(    
[0] => 23    
[1] => 27    
[2] => 28    
[3] => 34    
)  

当$v=33时  
Array    
(    
[4] => 33    
)


给你一个大大的么么给你一个大大的么么2612 days ago1090

reply all(3)I'll reply

  • 易风课堂

    易风课堂2017-11-17 21:15:56

    Because you are using a global variable, it will not be destroyed during the running of the entire program. Generally, it will appear when you call this method in a piece of code. If it is only called once in the page, there is no problem in writing it this way.

    You can change your mind and change the recursive call below. Try not to use global ones. Here, you can directly use an array variable to handle it.

    reply
    1
  • 给你一个大大的么么

    Thank you, I added a & in getAllID($uid) getAllID(&$uid) will do the trick.

    给你一个大大的么么 · 2017-11-20 10:05:45
  • 给你一个大大的么么

    给你一个大大的么么2017-11-17 14:51:05

    Can you please help me? I’ve been stuck at work for a long time

    reply
    1
  • Cancelreply