A simple and lightweight PHP library for manipulating nested structures — arrays, objects and JSON strings interchangeably.
| Method | Description |
|---|---|
getdiff() |
Returns the difference between two structures |
walker() |
Walks recursively through a structure, allowing edits and deletions |
structMerge() |
Merges two structures (first argument takes precedence) |
createDynamicallyObjects() |
Creates nested keys dynamically |
getDynamicallyValue() |
Reads a value by dynamic key path |
setDynamicallyValue() |
Sets a value by dynamic key path |
All methods accept and return any of the three supported structure types:
"jsonstring" | "object" | "array"
- PHP >= 8.1
composer require lukascivil/treewalker<?php
$treeWalker = new TreeWalker([
"debug" => false, // true = append execution time to output
"returntype" => "array" // "jsonstring" | "object" | "array"
]);Returns the difference between two structures, split into new, removed and edited keys.
<?php
$struct1 = ["casa" => 1, "b" => "5", "cafeina" => ["ss" => "ddd"], "oi" => 5];
$struct2 = ["casa" => 2, "cafeina" => ["ss" => "dddd"], "oi2" => 5];
$treeWalker->getdiff($struct1, $struct2, false); // false = flat slash-delimited keysOutput:
{
"new": {
"b": "5",
"oi": 5
},
"removed": {
"oi2": 5
},
"edited": {
"casa": {
"oldvalue": 2,
"newvalue": 1
},
"cafeina/ss": {
"oldvalue": "dddd",
"newvalue": "ddd"
}
}
}Pass true as the third argument to get nested output instead of slash-delimited keys:
<?php
$treeWalker->getdiff($struct1, $struct2, true); // true = nested outputWalks recursively through the structure. The callback receives the parent array, the current key and the current value by reference, allowing in-place modification or deletion.
<?php
$struct = ["casa" => 2, "cafeina" => ["ss" => ["ff" => 21, "ff1" => 22]], "oi2" => 5];
$treeWalker->walker($struct, function (&$struct, $key, &$value) {
if ($key === "ff") {
unset($struct[$key]); // delete node
}
if ($key === "ff1") {
$value = ["son" => "tiago"]; // replace value
}
});Output:
{"casa": 2, "cafeina": {"ss": {"ff1": {"son": "tiago"}}}, "oi2": 5}Merges two structures. Values from the first argument take precedence over the second.
<?php
$struct1 = ["casa" => 1, "b" => "5", "cafeina" => ["ss1" => "1", "ss2" => "2"], "oi" => 5];
$struct2 = ["casa" => 2, "cafeina" => ["ss" => ["ff" => 21, "ff1" => 22]], "oi2" => 5, "ss" => "dddddf"];
$treeWalker->structMerge($struct1, $struct2, true); // true = nested outputOutput:
{"casa": 1, "b": "5", "cafeina": {"ss1": "1", "ss2": "2", "ss": {"ff": 21, "ff1": 22}}, "oi": 5, "oi2": 5, "ss": "dddddf"}Creates nested empty objects from a dynamic array of keys.
<?php
$struct = ["casa" => 1, "b" => "5", "cafeina" => ["ss" => "ddd"], "oi" => 5];
$treeWalker->createDynamicallyObjects($struct, [1, 2, 5, 9, 10, 11]);Output:
{
"casa": 1,
"b": "5",
"cafeina": {"ss": "ddd"},
"oi": 5,
"1": {
"2": {
"5": {
"9": {
"10": {
"11": {}
}
}
}
}
}
}Reads a value from a structure using a dynamic key path.
<?php
$struct = ["casa" => 2, "cafeina" => ["ss" => ["ff" => 21, "ff1" => 22]], "oi2" => 5];
// Static access
$struct["cafeina"]["ss"];
// Dynamic access
$treeWalker->getDynamicallyValue($struct, ["cafeina", "ss"]);Output:
{"ff": 21, "ff1": 22}Sets a value in a structure using a dynamic key path.
<?php
$struct = ["casa" => 2, "cafeina" => ["ss" => ["ff" => 21, "ff1" => 22]], "oi2" => 5];
// Static access
$struct["cafeina"]["ss"] = "newvalue";
// Dynamic access
$treeWalker->setDynamicallyValue($struct, ["cafeina", "ss"], "newvalue");Output:
{"casa": 2, "cafeina": {"ss": "newvalue"}, "oi2": 5}composer install # install dev dependencies
composer test # run tests with coverage
composer check-format # PSR-2 lint
composer format # PSR-2 autofixIf you need the JavaScript equivalent for client-side structure comparison, see JsonDifference.
MIT © Lucas Cordeiro da Silva