{"id":21874546,"url":"https://github.com/smoren/tree-tools-php","last_synced_at":"2025-04-15T01:24:15.362Z","repository":{"id":65202911,"uuid":"587331247","full_name":"Smoren/tree-tools-php","owner":"Smoren","description":"PHP library for working with trees","archived":false,"fork":false,"pushed_at":"2024-12-18T21:15:49.000Z","size":24,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-28T13:21:16.199Z","etag":null,"topics":["php","tree","tree-builder","tree-structure","tree-walker"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Smoren.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2023-01-10T14:03:17.000Z","updated_at":"2024-12-18T21:15:53.000Z","dependencies_parsed_at":"2023-01-13T15:57:57.605Z","dependency_job_id":null,"html_url":"https://github.com/Smoren/tree-tools-php","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Smoren%2Ftree-tools-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Smoren%2Ftree-tools-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Smoren%2Ftree-tools-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Smoren%2Ftree-tools-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Smoren","download_url":"https://codeload.github.com/Smoren/tree-tools-php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248986923,"owners_count":21194142,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["php","tree","tree-builder","tree-structure","tree-walker"],"created_at":"2024-11-28T07:12:40.826Z","updated_at":"2025-04-15T01:24:15.351Z","avatar_url":"https://github.com/Smoren.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHP Tree Tools\n\n![Packagist PHP Version Support](https://img.shields.io/packagist/php-v/smoren/tree-tools)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/Smoren/tree-tools-php/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/Smoren/tree-tools-php/?branch=master)\n[![Coverage Status](https://coveralls.io/repos/github/Smoren/tree-tools-php/badge.svg?branch=master)](https://coveralls.io/github/Smoren/tree-tools-php?branch=master)\n![Build and test](https://github.com/Smoren/tree-tools-php/actions/workflows/test_master.yml/badge.svg)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\nLibrary for working with trees.\n\n## How to install to your project\n```\ncomposer require smoren/tree-tools\n```\n\n## Quick reference\n\n#### Tree Walker\n| Reducer                                           | Description                                | Code Snippet                                                     |\n|---------------------------------------------------|--------------------------------------------|------------------------------------------------------------------|\n| [`traverseDepthFirst`](#Traverse-Depth-First)     | Iterates a tree using depth-first search   | `TreeWalker::traverseDepthFirst($data, $childrenContainerKey)`   |\n| [`traverseBreadthFirst`](#Traverse-Breadth-First) | Iterates a tree using breadth-first search | `TreeWalker::traverseBreadthFirst($data, $childrenContainerKey)` |\n\n#### Tree Builder\n| Reducer           | Description                              | Code Snippet                                                                                              |\n|-------------------|------------------------------------------|-----------------------------------------------------------------------------------------------------------|\n| [`build`](#Build) | Builds a tree from given flat collection | `TreeBuilder::build($collection, $idField, $parentIdField, $childrenContainerField, $itemContainerField)` |\n\n## Usage\n\n### Tree Walker\n\n#### Traverse Depth First\n\nIterates a tree like a flat collection using depth-first traversal.\n\n```TreeWalker::traverseDepthFirst(iterable $data, ?string $childrenContainerKey = null): Generator```\n\nIf `$childrenContainerKey` is not null looks for children items using by this key only.\n\nOtherwise, considers any subarray to contain children.\n\n```php\nuse Smoren\\TreeTools\\TreeWalker;\n\n$tree = [\n    [\n        'id' =\u003e 1,\n        'children' =\u003e [\n            ['id' =\u003e 11],\n            [\n                'id' =\u003e 12,\n                'children' =\u003e [\n                    ['id' =\u003e 121],\n                    ['id' =\u003e 122],\n                ],\n            ],\n        ],\n    ],\n    [\n        'id' =\u003e 2,\n        'children' =\u003e [\n            ['id' =\u003e 21],\n        ],\n    ],\n    ['id' =\u003e 3],\n];\n\n$result = [];\n\nforeach(TreeWalker::traverseDepthFirst($tree) as $item) {\n    $result[] = $item['id'];\n}\nvar_dump($result);\n// [1, 11, 12, 121, 122, 2, 21, 3]\n```\n\n#### Traverse Breadth First\n\nIterates a tree like a flat collection using depth-breadth traversal.\n\n```TreeWalker::traverseBreadthFirst(iterable $data, ?string $childrenContainerKey = null): Generator```\n\nIf `$childrenContainerKey` is not null looks for children items using by this key only.\n\nOtherwise, considers any subarray to contain children.\n\n```php\nuse Smoren\\TreeTools\\TreeWalker;\n\n$tree = [\n    [\n        'id' =\u003e 1,\n        'children' =\u003e [\n            ['id' =\u003e 11],\n            [\n                'id' =\u003e 12,\n                'children' =\u003e [\n                    ['id' =\u003e 121],\n                    ['id' =\u003e 122],\n                ],\n            ],\n        ],\n    ],\n    [\n        'id' =\u003e 2,\n        'children' =\u003e [\n            ['id' =\u003e 21],\n        ],\n    ],\n    ['id' =\u003e 3],\n];\n\n$result = [];\n\nforeach(TreeWalker::traverseBreadthFirst($tree) as $item) {\n    $result[] = $item['id'];\n}\nvar_dump($result);\n// [1, 2, 3, 11, 12, 21, 121, 122]\n```\n\n### Tree Builder\n\n#### Build\n\nBuilds a tree from given flat collection of items with relations.\n\n```\nTreeBuilder::build(\n    iterable $collection,\n    string $idField = 'id',\n    string $parentIdField = 'parent_id',\n    string $childrenContainerField = 'children',\n    string $itemContainerField = 'item'\n): array\n```\n\n```php\nuse Smoren\\TreeTools\\TreeBuilder;\n\n$input = [\n    ['id' =\u003e 1, 'name' =\u003e 'Item 1', 'parent_id' =\u003e null],\n    ['id' =\u003e 2, 'name' =\u003e 'Item 1.1', 'parent_id' =\u003e 1],\n    ['id' =\u003e 3, 'name' =\u003e 'Item 1.2', 'parent_id' =\u003e 1],\n    ['id' =\u003e 4, 'name' =\u003e 'Item 1.1.1', 'parent_id' =\u003e 2],\n    ['id' =\u003e 5, 'name' =\u003e 'Item 2', 'parent_id' =\u003e null],\n    ['id' =\u003e 6, 'name' =\u003e 'Item 3', 'parent_id' =\u003e null],\n    ['id' =\u003e 7, 'name' =\u003e 'Item 3.1', 'parent_id' =\u003e 6],\n    ['id' =\u003e 8, 'name' =\u003e 'Item 3.2', 'parent_id' =\u003e 6],\n];\n\n$tree = TreeBuilder::build($input);\nprint_r($tree);\n/*\n[\n    [\n        'id' =\u003e 1,\n        'name' =\u003e 'Item 1',\n        'parent_id' =\u003e null,\n        'children' =\u003e [\n            [\n                'id' =\u003e 2,\n                'name' =\u003e 'Item 1.1',\n                'parent_id' =\u003e 1,\n                'children' =\u003e [\n                    [\n                        'id' =\u003e 4,\n                        'name' =\u003e 'Item 1.1.1',\n                        'parent_id' =\u003e 2,\n                        'children' =\u003e [],\n                    ]\n                ],\n            ],\n            [\n                'id' =\u003e 3,\n                'name' =\u003e 'Item 1.2',\n                'parent_id' =\u003e 1,\n                'children' =\u003e [],\n            ],\n        ],\n    ],\n    [\n        'id' =\u003e 5,\n        'name' =\u003e 'Item 2',\n        'parent_id' =\u003e null,\n        'children' =\u003e [],\n    ],\n    [\n        'id' =\u003e 6,\n        'name' =\u003e 'Item 3',\n        'parent_id' =\u003e null,\n        'children' =\u003e [\n            [\n                'id' =\u003e 7,\n                'name' =\u003e 'Item 3.1',\n                'parent_id' =\u003e 6,\n                'children' =\u003e [],\n            ],\n            [\n                'id' =\u003e 8,\n                'name' =\u003e 'Item 3.2',\n                'parent_id' =\u003e 6,\n                'children' =\u003e [],\n            ],\n        ]\n    ],\n]\n*/\n\n```\n\n## Unit testing\n```\ncomposer install\ncomposer test-init\ncomposer test\n```\n\n## License\n\nTree Tools PHP is licensed under the MIT License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmoren%2Ftree-tools-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsmoren%2Ftree-tools-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmoren%2Ftree-tools-php/lists"}