Recommend this page to a friend! |
Library for working with trees.
composer require smoren/tree-tools
| Reducer | Description | Code Snippet |
|---------------------------------------------------|--------------------------------------------|------------------------------------------------------------------|
| traverseDepthFirst
| Iterates a tree using depth-first search | TreeWalker::traverseDepthFirst($data, $childrenContainerKey)
|
| traverseBreadthFirst
| Iterates a tree using breadth-first search | TreeWalker::traverseBreadthFirst($data, $childrenContainerKey)
|
| Reducer | Description | Code Snippet |
|-------------------|------------------------------------------|-----------------------------------------------------------------------------------------------------------|
| build
| Builds a tree from given flat collection | TreeBuilder::build($collection, $idField, $parentIdField, $childrenContainerField, $itemContainerField)
|
Iterates a tree like a flat collection using depth-first traversal.
If `$childrenContainerKey` is not null looks for children items using by this key only.
Otherwise, considers any subarray to contain children.
use Smoren\TreeTools\TreeWalker;
$tree = [
[
'id' => 1,
'children' => [
['id' => 11],
[
'id' => 12,
'children' => [
['id' => 121],
['id' => 122],
],
],
],
],
[
'id' => 2,
'children' => [
['id' => 21],
],
],
['id' => 3],
];
$result = [];
foreach(TreeWalker::traverseDepthFirst($tree) as $item) {
$result[] = $item['id'];
} var_dump($result); // [1, 11, 12, 121, 122, 2, 21, 3]
#### Traverse Breadth First
Iterates a tree like a flat collection using depth-breadth traversal.
If $childrenContainerKey
is not null looks for children items using by this key only.
Otherwise, considers any subarray to contain children.
use Smoren\TreeTools\TreeWalker;
$tree = [
[
'id' => 1,
'children' => [
['id' => 11],
[
'id' => 12,
'children' => [
['id' => 121],
['id' => 122],
],
],
],
],
[
'id' => 2,
'children' => [
['id' => 21],
],
],
['id' => 3],
];
$result = [];
foreach(TreeWalker::traverseBreadthFirst($tree) as $item) {
$result[] = $item['id'];
}
var_dump($result);
// [1, 2, 3, 11, 12, 21, 121, 122]
Builds a tree from given flat collection of items with relations.
TreeBuilder::build(
iterable $collection,
string $idField = 'id',
string $parentIdField = 'parent_id',
string $childrenContainerField = 'children',
string $itemContainerField = 'item'
): array
use Smoren\TreeTools\TreeBuilder;
$input = [
['id' => 1, 'name' => 'Item 1', 'parent_id' => null],
['id' => 2, 'name' => 'Item 1.1', 'parent_id' => 1],
['id' => 3, 'name' => 'Item 1.2', 'parent_id' => 1],
['id' => 4, 'name' => 'Item 1.1.1', 'parent_id' => 2],
['id' => 5, 'name' => 'Item 2', 'parent_id' => null],
['id' => 6, 'name' => 'Item 3', 'parent_id' => null],
['id' => 7, 'name' => 'Item 3.1', 'parent_id' => 6],
['id' => 8, 'name' => 'Item 3.2', 'parent_id' => 6],
];
$tree = TreeBuilder::build($input);
print_r($tree);
/*
[
[
'id' => 1,
'name' => 'Item 1',
'parent_id' => null,
'children' => [
[
'id' => 2,
'name' => 'Item 1.1',
'parent_id' => 1,
'children' => [
[
'id' => 4,
'name' => 'Item 1.1.1',
'parent_id' => 2,
'children' => [],
]
],
],
[
'id' => 3,
'name' => 'Item 1.2',
'parent_id' => 1,
'children' => [],
],
],
],
[
'id' => 5,
'name' => 'Item 2',
'parent_id' => null,
'children' => [],
],
[
'id' => 6,
'name' => 'Item 3',
'parent_id' => null,
'children' => [
[
'id' => 7,
'name' => 'Item 3.1',
'parent_id' => 6,
'children' => [],
],
[
'id' => 8,
'name' => 'Item 3.2',
'parent_id' => 6,
'children' => [],
],
]
],
]
*/
composer install
composer test-init
composer test
PHP Tree Tools is licensed under the MIT License.
Classes of Smoren Freelight | > | PHP Tree tools | > | Download .zip .tar.gz | > | Support forum | > | Blog | > | Latest changes |
|
Groups | Applications | Files |
Groups |
Algorithms | Numerical and statistical algorithms | View top rated classes |
Libraries | Frameworks and libraries of cooperating classes | View top rated classes |
Data types | Modeling and manipulating data types | View top rated classes |
PHP 7 | Classes using PHP 7 specific features | View top rated classes |
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.
Files |
File | Role | Description | ||
---|---|---|---|---|
.github (1 directory) | ||||
src (2 files) | ||||
tests (3 files, 2 directories) | ||||
.scrutinizer.yml | Data | Auxiliary data | ||
codeception.yml | Data | Auxiliary data | ||
composer.json | Data | Auxiliary data | ||
LICENSE | Lic. | License text | ||
phpcs.xml | Data | Auxiliary data | ||
phpstan.neon | Data | Auxiliary data | ||
README.md | Doc. | Documentation |
Files | / | src |
File | Role | Description |
---|---|---|
TreeBuilder.php | Class | Class source |
TreeWalker.php | Class | Class source |
Files | / | tests |
File | Role | Description | ||
---|---|---|---|---|
unit (3 directories) | ||||
_support (1 file) | ||||
coding_standard.xml | Data | Auxiliary data | ||
unit.suite.yml | Data | Auxiliary data | ||
_bootstrap.php | Aux. | Auxiliary script |
Files | / | tests | / | unit |
File | Role | Description | ||
---|---|---|---|---|
Fixture (4 files) | ||||
TreeBuilder (3 files) | ||||
TreeWalker (2 files) |
Files | / | tests | / | unit | / | Fixture |
File | Role | Description |
---|---|---|
ArrayIteratorFixture.php | Class | Class source |
GeneratorFixture.php | Class | Class source |
IteratorAggregateFixture.php | Class | Class source |
TreeItemFixture.php | Class | Class source |
Files | / | tests | / | unit | / | TreeBuilder |
File | Role | Description |
---|---|---|
ArrayTest.php | Class | Class source |
ClassObjectTest.php | Class | Class source |
StdObjectTest.php | Class | Class source |
Files | / | tests | / | unit | / | TreeWalker |
File | Role | Description |
---|---|---|
TraverseBreadthFirstTest.php | Class | Class source |
TraverseDepthFirstTest.php | Class | Class source |
Download all files: tree-tools-php.tar.gz tree-tools-php.zip NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.
|
Files |
File | Role | Description | ||
---|---|---|---|---|
.github (1 directory) | ||||
src (2 files) | ||||
tests (3 files, 2 directories) | ||||
.scrutinizer.yml | Data | Auxiliary data | ||
codeception.yml | Data | Auxiliary data | ||
composer.json | Data | Auxiliary data | ||
LICENSE | Lic. | License text | ||
phpcs.xml | Data | Auxiliary data | ||
phpstan.neon | Data | Auxiliary data | ||
README.md | Doc. | Documentation |
Files | / | src |
File | Role | Description |
---|---|---|
TreeBuilder.php | Class | Class source |
TreeWalker.php | Class | Class source |
Files | / | tests |
File | Role | Description | ||
---|---|---|---|---|
unit (3 directories) | ||||
_support (1 file) | ||||
coding_standard.xml | Data | Auxiliary data | ||
unit.suite.yml | Data | Auxiliary data | ||
_bootstrap.php | Aux. | Auxiliary script |
Files | / | tests | / | unit |
File | Role | Description | ||
---|---|---|---|---|
Fixture (4 files) | ||||
TreeBuilder (3 files) | ||||
TreeWalker (2 files) |
Files | / | tests | / | unit | / | Fixture |
File | Role | Description |
---|---|---|
ArrayIteratorFixture.php | Class | Class source |
GeneratorFixture.php | Class | Class source |
IteratorAggregateFixture.php | Class | Class source |
TreeItemFixture.php | Class | Class source |
Files | / | tests | / | unit | / | TreeBuilder |
File | Role | Description |
---|---|---|
ArrayTest.php | Class | Class source |
ClassObjectTest.php | Class | Class source |
StdObjectTest.php | Class | Class source |
Files | / | tests | / | unit | / | TreeWalker |
File | Role | Description |
---|---|---|
TraverseBreadthFirstTest.php | Class | Class source |
TraverseDepthFirstTest.php | Class | Class source |
Download all files: tree-tools-php.tar.gz tree-tools-php.zip NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.
|