GithubHelp home page GithubHelp logo

datatree's Introduction

DataTree

This simple library/class allows you to get a tree structure using a one-dimensional array.

Note : It took approximately 1.10 seconds for this library to process an array with 1,000,000 elements, using over 400MB of memory. Keep in mind that the larger the data you are processing, the more memory and time you will need.

Installation

composer require muhammetsafak/data-tree

Usage

require_once "vendor/autoload.php";
use MuhammetSafak\DataTree\TreeGenerator;

$data = [
    ['ID' => 1, 'parent_id' => 0, 'random_name' => 'Random #1'],
    ['ID' => 2, 'parent_id' => 0, 'random_name' => 'Random #2'],
    ['ID' => 3, 'parent_id' => 1, 'random_name' => 'Random #3'],
    ['ID' => 4, 'parent_id' => 2, 'random_name' => 'Random #4'],
    ['ID' => 5, 'parent_id' => 0, 'random_name' => 'Random #5'],
    ['ID' => 6, 'parent_id' => 2, 'random_name' => 'Random #6'],
];

$tree = (new TreeGenerator($data))
    ->setRelation('id', 'parentId')
    ->setReName('ID', 'id')
    ->setReName('parent_id', 'parentId')
    ->setReName('random_name', 'name')
    ->setChildNodeName('@childrens')
    ->toTree();

print_r($tree);

Output :

Array
(
    [0] => Array
        (
            [id] => 1
            [parentId] => 0
            [name] => Random #1
            [@childrens] => Array
                (
                    [0] => Array
                        (
                            [id] => 3
                            [parentId] => 1
                            [name] => Random #3
                        )

                )

        )

    [1] => Array
        (
            [id] => 2
            [parentId] => 0
            [name] => Random #2
            [@childrens] => Array
                (
                    [0] => Array
                        (
                            [id] => 4
                            [parentId] => 2
                            [name] => Random #4
                        )

                    [1] => Array
                        (
                            [id] => 6
                            [parentId] => 2
                            [name] => Random #6
                        )

                )

        )

    [2] => Array
        (
            [id] => 5
            [parentId] => 0
            [name] => Random #5
        )

)

Credits

License

Copyright © 2022 MIT License

datatree's People

Contributors

muhammetsafak avatar

Watchers

 avatar

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.