2017 © Pedro Peláez
 

library tree

A simple helper package to allow using Trees on PHP

image

danielcosta/tree

A simple helper package to allow using Trees on PHP

  • Thursday, April 5, 2018
  • by danielcosta
  • Repository
  • 1 Watchers
  • 0 Stars
  • 20 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 43 % Grown

The README.md

PHP Helper - Tree

Latest Version on Packagist ![Software License][ico-license] Build Status ![Coverage Status][ico-scrutinizer] Quality Score ![Total Downloads][ico-downloads], (*1)

Quick Installation

composer require danielcosta/tree

Usage

To use, just get an array like bellow, (*2)

$flatArray = [
    [
        'id' => 1,
        'name' => 'Level 1',
        'parentId' => 0,
        'nodes' => [],
    ],
    [
        'id' => 3,
        'name' => 'Level 3',
        'parentId' => 2,
        'nodes' => [],
    ],
    [
        'id' => 2,
        'name' => 'Level 2',
        'parentId' => 1,
        'nodes' => [],
    ],
];

And pass it to the helper Tree, using Tree::makeFromFlatArray($flatArray);. The result will be exactly this:, (*3)

$result = Tree::makeFromFlatArray($flatArray);
print_r($result);
/*
Array
(
    [id] => 1
    [name] => Level 1
    [parentId] => 0
    [nodes] => Array
        (
            [0] => Array
                (
                    [id] => 2
                    [name] => Level 2
                    [parentId] => 1
                    [nodes] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 3
                                    [name] => Level 3
                                    [parentId] => 2
                                    [nodes] => Array
                                        (
                                        )
                                )
                        )
                )
        )
)
*/

Optional parameters to this method are:, (*4)

  • $parentId = 0 - when you want to set the first level of the return
  • string $key = 'id' - the primary key on your flat array
  • string $parentKey = 'parentId' - the parent key on your flat array
  • string $childKey = 'nodes' - where to put the child nodes

Buy me a coffee

paypal, (*5)

The Versions

05/04 2018

dev-master

9999999-dev

A simple helper package to allow using Trees on PHP

  Sources   Download

MIT

The Requires

  • php ^7.2

 

The Development Requires

05/04 2018

1.0.0

1.0.0.0

A simple helper package to allow using Trees on PHP

  Sources   Download

MIT

The Development Requires