2015-11-24 16:58

限制函数在php codeigniter中递归的次数


I want to generate a multilevel hierarchical select option to select parent or any of its child categories in codeigniter. I want to limit the hierarchy level to 3. i.e the option must show parent, its first level child and its second level child with child category indented to the right of parent. I've no problem going through all the hierarchy level. But I can't generate the select option upto a particular hierarchy level. Three levels of hierarchy is what I need in my case. Can anyone please help me.

Here is what I tried so far:

$category = $this->get_category();

function get_category() {
    $this->db->order_by('parent_id', 'DESC');
    $this->db->select('id, name, parent_id');
    return $this->db->get('tbl_job_category')->result_array();

*$category contains the following json data obtained from DB
$category = [{"id":"20","name":"MVC","parent_id":"16"},
{"id":"12","name":"Car Driver","parent_id":"13"},
{"id":"17","name":"Computer Operator","parent_id":"1"},
{"id":"1","name":"Information Technology","parent_id":"0"}];

$category_options = $this->multilevel_select($category);

function multilevel_select($array,$parent_id = 0,$parents = array()) {
    static $i=0;
        foreach ($array as $element) {
            if (($element['parent_id'] != 0) && !in_array($element['parent_id'],$parents)) {
                $parents[] = $element['parent_id'];

    $menu_html = '';
    foreach($array as $element){
            $menu_html .= '<option>';
            for($j=0; $j<$i; $j++) {
                $menu_html .= '&mdash;';
            $menu_html .= $element['name'].'</option>';
            if(in_array($element['id'], $parents)){
                $menu_html .= $this->multilevel_select($array, $element['id'], $parents);
    return $menu_html;

echo $category_options;
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dqqn32019 dqqn32019 6年前

    Limiting the recursion involves three steps :

    • Initializing a counter variable passed as parameter to the recursive function.
    • Testing the counter value against the desired boundary prior to recursing
    • Passing an incremented counter value in case of recursive call.

    In your case that would be the $level variable below :

    function multilevel_select($array,$parent_id = 0,$parents = array(), $level=0) {
        // ....
                if(in_array($element['id'], $parents) && $level < 2){ // your boundary here, 2 for third nesting level from root
                    $menu_html .= $this->multilevel_select($array, $element['id'], $parents, $level+1);
        return $menu_html;
    点赞 评论 复制链接分享