function get_child_id($data, $parent_id = '0') { static $child; foreach ($data as $value) { if ($value['parent_id'] == $parent_id) { $child .= ',' . $value['cat_id']; $this->get_child_id($data, $value['cat_id'], $level + 1); } } return $child; }
回复讨论(解决方案)
循环调用你这个方法就可以了。
$parent_ids = array(1,2,3,4);$resul t= array();foreach($parent_ids as $parent_id){ $result[] = get_child_id($data, $parent_id);}function get_child_id($data, $parent_id = '0') { static $child; foreach ($data as $value) { if ($value['parent_id'] == $parent_id) { $child .= ',' . $value['cat_id']; $this->get_child_id($data, $value['cat_id'], $level + 1); } } return $child; } 对每个 父ID 都调用一下 get_child_id 不就行了?
不过 static $child; 会在多次调用时出问题的
多次循环 get_child_id会出现子栏目ID重复出现的情况
#2 我已经告诉你会出问题了
症结在 static $child; !
function get_child_id($data, $parent_id = '0') { $child = ''; foreach ($data as $value) { if ($value['parent_id'] == $parent_id) { $child .= ',' . $value['cat_id']; $child .= $this->get_child_id($data, $value['cat_id'], $level + 1); } } return $child; } 这个我发现了,但是还是会出现子栏目ID重复的情况
贴出你的测试数据,应该是 id 不唯一(没有死循环已经对你很客气了)
贴出你的测试数据,应该是 id 不唯一(没有死循环已经对你很客气了)
贴出你的测试数据,应该是 id 不唯一(没有死循环已经对你很客气了)
就是那个static问题,打开页面太多了,调试错了,不好意思啊版主!
我 #4 不是给你代码了吗?
我 #4 不是给你代码了吗?
看到了,也调试好了。谢谢版主