Git Product home page Git Product logo

composer-tree's Introduction

composer-tree:php不使用递归调用将数组树形化高效代码

使用方法

#安装 composer require jiangzg/composer-tree:1.0.0

include_once __DIR__.'/../vendor/autoload.php';

$testAyy = [
    [
        'id'=>1,
        'parentid'=>0,
        'name'=>'第1个'
    ],
    [
        'id'=>2,
        'parentid'=>1,
        'name'=>'第2个'
    ],
    [
        'id'=>3,
        'parentid'=>2,
        'name'=>'第3个'
    ],
    [
        'id'=>4,
        'parentid'=>1,
        'name'=>'第4个'
    ],
    [
        'id'=>5,
        'parentid'=>0,
        'name'=>'第5个'
    ],
    [
        'id'=>6,
        'parentid'=>5,
        'name'=>'第6个'
    ],
    [
        'id'=>7,
        'parentid'=>6,
        'name'=>'第7个'
    ],
    [
        'id'=>8,
        'parentid'=>5,
        'name'=>'第8个'
    ],
    [
        'id'=>9,
        'parentid'=>8,
        'name'=>'第9个'
    ],
    [
        'id'=>10,
        'parentid'=>9,
        'name'=>'第10个'
    ],
];

$tree = new \JiangZG\Tree\Tree();

$result = $tree->createTree($testAyy);

print_r($result);

输出结果

Array
(
    [0] => Array
        (
            [id] => 1
            [parentid] => 0
            [name] => 第1个
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [parentid] => 1
                            [name] => 第2个
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 3
                                            [parentid] => 2
                                            [name] => 第3个
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [id] => 4
                            [parentid] => 1
                            [name] => 第4个
                        )

                )

        )

    [1] => Array
        (
            [id] => 5
            [parentid] => 0
            [name] => 第5个
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 6
                            [parentid] => 5
                            [name] => 第6个
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 7
                                            [parentid] => 6
                                            [name] => 第7个
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [id] => 8
                            [parentid] => 5
                            [name] => 第8个
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 9
                                            [parentid] => 8
                                            [name] => 第9个
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 10
                                                            [parentid] => 9
                                                            [name] => 第10个
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.