codeigniter 另一个PHP递归函数用于递归对象数组

wfveoks0  于 2022-12-06  发布在  PHP
关注(0)|答案(1)|浏览(130)

我选择从父id= 0开始的所有category元素,并将其父id值等于父节点的id的项放入。

public function get_categories($table_name, $parent_id = 0)
{

$this->db->where('`parent`', $parent);

$parent = $this->db->get($table_name);

$categories = $parent->result();

$i = 0;
foreach ($categories as $p_cat) {

    $categories[$i]->sub = $this->sub_categories($table_name, $p_cat->id);
    $i++;
}
return $categories;
}

public function sub_categories($table_name, $id)
{

    $this->db->where('`parent`', $id);

    $child = $this->db->get($table_name);
    $categories = $child->result();
    $i = 0;
    foreach ($categories as $p_cat) {

        $categories[$i]->sub = $this->sub_categories($table_name, $p_cat->id);
        $i++;
    }
    return $categories;
}

这是my_categories数据表的输出。$categories内容如下:

Array ( 
    [0] => stdClass Object ( 
            [id] => 1 
            [name] => XMLELEMENT10 
            [parent] => 0 
            [sub] => Array ( 
                [0] => stdClass Object ( 
                    [id] => 13 
                    [name] => XMLELEMENT113 
                    [parent] => 1 
                    [sub] => Array ( ) 
                ) 
                [1] => stdClass Object ( 
                    [id] => 14 
                    [name] => XMLELEMENT114 
                    [parent] => 1 
                    [sub] => Array ( ) 
                ) 
                [2] => stdClass Object ( 
                    [id] => 15 
                    [name] => XMLELEMENT115
                    [parent] => 1 
                    [sub] => Array ( ) ) 
                [3] => stdClass Object ( 
                    [id] => 16 
                    [name] => XMLELEMENT116 
                    [parent] => 1 
                    [sub] => Array ( ) 
                ) 
            ) 
        ) 
    [1] => stdClass Object ( 
            [id] => 2 
            [name] => XMLELEMENT20 
            [parent] => 0 
            [sub] => Array ( 
                [0] => stdClass Object ( 
                    [id] => 5 
                    [name] => XMLELEMENT52 
                    [parent] => 2 
                    [sub] => Array ( ) 
                ) 
            ) 
        ) 
    [2] => stdClass Object ( 
            [id] => 3 
            [name] => XMLELEMENT30 
            [parent] => 0 
            [sub] => Array ( 
                [0] => stdClass Object ( 
                    [id] => 6 
                    [name] => XMLELEMENT63 
                    [parent] => 3 
                    [sub] => Array ( ) 
                ) 
                [1] => stdClass Object ( 
                    [id] => 7 
                    [name] => XMLELEMENT73 
                    [parent] => 3 
                    [sub] => Array ( ) 
                ) 
                [2] => stdClass Object ( 
                    [id] => 8 
                    [name] => XMLELEMENT83 
                    [parent] => 3 
                    [sub] => Array ( ) 
                ) 
                [3] => stdClass Object ( 
                    [id] => 9 
                    [name] => XMLELEMENT93 
                    [parent] => 3 
                    [sub] => Array ( ) 
                ) 
            ) 
        ) 
        [3] => stdClass Object ( 
            [id] => 4 
            [name] => XMLELEMENT40
            [parent] => 0 
            [sub] => Array ( 
                [0] => stdClass Object ( 
                    [id] => 10 
                    [name] => XMLELEMENT104 
                    [parent] => 4 
                    [sub] => Array ( ) 
                ) 
                [1] => stdClass Object ( 
                    [id] => 11 
                    [name] => XMLELEMENT2017114 
                    [parent] => 4 
                    [sub] => Array ( ) 
                ) 
                [2] => stdClass Object ( 
                    [id] => 12 
                    [name] => XMLELEMENT122 
                    [parent] => 4 
                    [sub] => Array ( ) 
                ) 
            ) 
        ) 
        [4] => stdClass Object ( 
            [id] => 17 
            [name] => XMLELEMENT170 
            [parent] => 0 
            [sub] => Array ( 
                [0] => stdClass Object ( 
                    [id] => 18 
                    [name] => XMLELEMENT1817 
                    [parent] => 17 
                    [sub] => Array ( ) 
                ) 
                [1] => stdClass Object ( 
                    [id] => 19 
                    [name] => XMLELEMENT1917 
                    [parent] => 17 
                    [sub] => Array ( ) 
                ) 
                [2] => stdClass Object ( 
                    [id] => 20 
                    [name] => XMLELEMENT2017 
                    [parent] => 17 
                    [sub] => Array ( ) 
                ) 
            ) 
        ) 
    )

问题很简单,但我不知道。我只需要列出所有项目,如

<ul>
    <li>root
     <!-- if has children -->
        <ul>
            <li>child
               <!-- if has children -->
                <ul>
                   <li>child of child
.
.
.

        </ul>
    </li>
</ul>

我无法编写简单递归函数。

avwztpqn

avwztpqn1#

非常简单的解决方案:

<?php
function printList(array $data){
    print "<ul>\n";
    foreach($data as $set){
        print "<li>$set->name";
        if(is_array($set->sub) && count($set->sub)){
            print "\n";
            printList($set->sub);
        }
        print "</li>\n";
    }
    print "</ul>\n";
}

#example data like your $categories array
$subexample = new stdClass;
$subexample->name = 'foobar';
$subexample->sub = array();
$example = new stdClass;
$example->name = 'foobar';
$example->sub = array($subexample);
$data = array($example,$example,$example);

printList($data);

相关问题