PHP code example of lukascivil / treewalker
1. Go to this page and download the library: Download lukascivil/treewalker library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
lukascivil / treewalker example snippets
$treeWalker = new TreeWalker([
"debug" => false, // true = append execution time to output
"returntype" => "array" // "jsonstring" | "object" | "array"
]);
$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 keys
$treeWalker->getdiff($struct1, $struct2, true); // true = nested output
$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
}
});
$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 output
$struct = ["casa" => 1, "b" => "5", "cafeina" => ["ss" => "ddd"], "oi" => 5];
$treeWalker->createDynamicallyObjects($struct, [1, 2, 5, 9, 10, 11]);
$struct = ["casa" => 2, "cafeina" => ["ss" => ["ff" => 21, "ff1" => 22]], "oi2" => 5];
// Static access
$struct["cafeina"]["ss"];
// Dynamic access
$treeWalker->getDynamicallyValue($struct, ["cafeina", "ss"]);
$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");
json
{"ff": 21, "ff1": 22}