在 php 函数中显示分层选择

发布于 2024-11-15 07:27:31 字数 4709 浏览 3 评论 0原文

我有一个如下所示的数组:

$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');

并且想在像这样的 PHP 函数中显示它,该函数调用一个函数并给出这个数组,

例如

$id=1; 
builder_tree($id); 

给我下面的数组,请帮助我

Array
(
    [0] => Array
        (
        [id] => 1
            [parent] => 0
            [title] => Tutorials
            [children] => Array
                (
                    [0] => Array
                        (
                [id] => 2
                            [parent] => 1
                            [title] => 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 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
                            [title] => 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
                                                                (
                                                                )
                                                        )                                               
                                                )
                                        )                               
                                )
                        )                               
                )
        )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城挽邺 2024-11-22 07:27:31

我意识到这是一个老问题,但我一直在寻找与原始海报所需类似的东西,并提出了以下内容。

希望这对未来的 Google 员工有所帮助。

build_tree_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;
        }

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;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文