{"id":24727057,"url":"https://github.com/philipp-riddle/xtraverse.php","last_synced_at":"2025-03-22T15:10:45.904Z","repository":{"id":57038766,"uuid":"420486673","full_name":"philipp-riddle/xtraverse.php","owner":"philipp-riddle","description":"PHP bundle which makes array traversing / inserting dead easy.","archived":false,"fork":false,"pushed_at":"2022-03-25T09:30:50.000Z","size":38,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-27T14:51:13.773Z","etag":null,"topics":["nested-arrays","nested-objects","php","php7","traverse","tree"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/philipp-riddle.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-10-23T18:04:10.000Z","updated_at":"2022-02-12T13:54:54.000Z","dependencies_parsed_at":"2022-08-24T00:51:20.583Z","dependency_job_id":null,"html_url":"https://github.com/philipp-riddle/xtraverse.php","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philipp-riddle%2Fxtraverse.php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philipp-riddle%2Fxtraverse.php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philipp-riddle%2Fxtraverse.php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philipp-riddle%2Fxtraverse.php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/philipp-riddle","download_url":"https://codeload.github.com/philipp-riddle/xtraverse.php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244973776,"owners_count":20541025,"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":["nested-arrays","nested-objects","php","php7","traverse","tree"],"created_at":"2025-01-27T14:50:18.400Z","updated_at":"2025-03-22T15:10:45.886Z","avatar_url":"https://github.com/philipp-riddle.png","language":"PHP","readme":"# XTraverse.php\n\nThis bundle makes it dead easy to traverse through nested arrays/objects in PHP. \n\n## Installation\n### Via Composer\n```terminal\ncomposer require phiil/xtraverse\n```\n### File download\nTo use all the functions of this package simply download the ```src/``` directory.\n\n## Getting started\n\n### Traversing paths\nPaths are how you specify where you want to traverse to inside the nested object. Let's assume you want to get the title of the Block with ID 1 inside the 'blocks' array.\n\n\nOur nested object:\n```php\n$data = [\n    'blocks' =\u003e [\n        [\n            'id' =\u003e 1,\n            'title' =\u003e 'First block',\n        ],\n    ],\n];\n```\n\nNow we specify which element we want:\n```php\n$path = 'blocks[1].title';\n```\n\nEvery step is delimited with a dot ('.') - if you want to query for an ID simply append it to the path with closed brackets.\n  \nNow let the traversing begin:\n\n```php\nuse Phiil\\XTraverse\\Traverser;\n\n$traverser = new Traverser();\n$title = $traverser-\u003etraverseData($path, $data, traverseArrayLimit: false); // we want a non-array value - pass \"false\" as the last argument or the service will throw an exception\n\necho $title;\n```\n\nThe above will output the following: ```First block```\n\n\n### Updating a nested value\nUpdating a value also works with paths:\n\n```php\nuse Phiil\\XTraverse\\Traverser;\n\n// We want to update the title of the block we previously traversed to\n$updatePath = 'blocks[1].title';\n\n$traverser = new Traverser();\n$data = $traverser-\u003eupdate($data, $path, 'New title')-\u003edata;\n```\n\n**Note:** The update method returns an object with the properties ```path```, ```data``` \u0026 ```insert```. Getting the data property from the object straight away is almost always the best option.\n\n### Working with IDs\nThe traverse service can auto-increment IDs - meaning if you insert a nested object like:\n\n```php\nuse Phiil\\XTraverse\\Traverser;\n\n$object = [\n    'id' =\u003e null,\n    'title' =\u003e 'Second block',\n];\n$traverser = new Traverser();\n$data = $traverser-\u003eupdate($object, 'blocks.$', $object)-\u003edata;\n```\n\nThe object inside ```$data``` will now have the ID of 2 (First Block: ID 1).\n\n**Note:** The ```path.$``` syntax can be used if you want to add a block to a non-associative (only numeric keys) array.\n\n## Running tests\n\nTo run tests run the following commands:\n```terminal\ncomposer install --dev\n./vendor/bin/phpunit tests/\n```\n\n## Problems? Issues?\nJust post them here on Github or contact me via email: [philipp@riddle.com](mailto:philipp@riddle.com). Feel free to contribute!","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphilipp-riddle%2Fxtraverse.php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphilipp-riddle%2Fxtraverse.php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphilipp-riddle%2Fxtraverse.php/lists"}