开发者

show hierarchical select in a php function

I have an array like below:

$cats = array();
$cats[1] = array('id' => 1,'parent' => 0, 'title' => 'Tutorials');
$cats[2] = array('id' => 2,'parent' => 1, 'title' => 'PHP');
$cats[3] = array('id' => 3,'parent' => 2, 'title' => 'OOP');
$cats[4] = array('id' => 4,'parent' => 2, 'title' => 'Tips');
$cats[5] = array('id' => 5,'parent' => 1, 'title' => 'JavaScript');
$cats[6] = array('id' => 6,'parent' => 5, 'title' => 'Basics');
$cats[7] = array('id' => 7,'parent' => 5, 'title' => 'Frameworks');
$cats[8] = array('id' => 8,'parent' => 7, 'title' => 'jQuery');
$cats[9] = array('id' => 9,'parent' => 7, 'title' => 'MooTools');
$cats[10] = array('id' => 10,'parent' => 0, 'title' => 'News');
$cats[11] = array('id' => 11,'parent' => 10, 'title' => 'PHP');
$cats[12] = array('id' => 12,'parent' => 10, 'title' => 'Wordpress');
$cats[13] = array('id' => 13,'parent' => 0, 'title' => 'New');

and want to show it in a PHP function like this that call an function and give this array,

for example

$id=1; 
builder_tree($id); 

and give me bellow array ,please help me

Array
(
    [0] => Array
        (
        [id] => 1
            [parent] => 0
            [title] => Tutorials
            [children] => Array
                (
                    [0] => Array
                        (
                [id] => 2
                            [parent] => 1
                            [titl开发者_如何学运维e] => PHP
                            [children] => Array
                                (
                                )
                        )
                    [1] => Array
                        (
                [id] => 3
                            [parent] => 1
                            [title] => PHP
                            [children] => Array
                                (
                                )
                        )
                    [2] => Array
                        (
                [id] => 5
                            [parent] => 1
                            [title] => JavaScript
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 6
                                            [parent] => 5
                                            [title] => PHP
                                            [children] => Array
                                                (
                                                )
                                        )
                                    [1] => Array
                                        (
                                            [id] => 7
                                            [parent] => 5
                                            [title] => PHP
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 8
                                                            [parent] => 7
                                                            [title] => jQuery
                                                            [children] => Array
                                                                (
                                                                )
                                                        )
                                                    [1] => Array
                                                        (
                                                            [id] => 9
                                                            [parent] => 7
                                                            [title] => MooTools
                                                            [children] => Array
                                                                (
                                                                )
                                                        )                                               
                                                )
                                        )                               
                                )
                        )                               
                )
        )


I realize this is an old question, but I was looking for something similar to what the original poster needed and came up with the following.

Hope this helps any future Googlers.

The build_tree_array() basically takes what the original poster posted above and coverts it to a hierarchical array.

function folder_has_children($rows, $id) {
    foreach ($rows as $row) {
        if ($row['child_of'] == $id)
            return true;
    }
    return false;
}
function build_tree_array(&$rows, $parent = 0) {
            foreach ($rows as $row) {
                if ($row['child_of'] == $parent) {
                    $result = $row;
                    if ($this->folder_has_children($rows, $row['id'])) {
                        $result['children'] = $this->build_tree_array($rows, $row['id']);
                    }

                    $out[] = $result;
                }
            }

            return $out;
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜