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



sh
    //walker() - Walk recursively through the structure

    $struct = array("casa"=>2, "cafeina"=>array("ss"=>array("ff"=>21, "ff1"=>22)), "oi2"=>5, "1"=>"", "ss"=>"dddddf");

    $treewalker->walker($struct, function(&$struct, $key, &$value) {
        //Removing element
        if ($key == "ff") {
            unset($struct[$key]);
        }

        //changing element
        if ($key == "ff1") {
            $value = array("son" => "tiago");
        }
    })

    Output:
    {"casa":2,"cafeina":{"ss":{"ff1":{"son":"tiago"}}},"oi2":5,"1":"","ss":"dddddf","time":"0 miliseconds"}

sh
    //getDynamicallyValue()

    $struct = array("casa"=>2, "cafeina"=>array("ss"=>array("ff"=>21, "ff1"=>22)), "oi2"=>5, "1"=>"", "ss"=>"dddddf");

    Static access:
    $struct["cafeina"]["ss"];

    Dynamic access:
    $treewalker->getDynamicallyValue($struct, array("cafeina","ss"));

    Output:
    {"ff":21,"ff1":22}
sh
    //setDynamicallyValue()

    $struct = array("casa"=>2, "cafeina"=>array("ss"=>array("ff"=>21, "ff1"=>22)), "oi2"=>5, "1"=>"", "ss"=>"dddddf");

    Static access:
    $struct["cafeina"]["ss"] = "newvalue";

    Dynamic access:
    $treewalker->setDynamicallyValue($struct, array("cafeina","ss"), "newvalue");

    Output:
    {"casa":2,"cafeina":{"ss":"newvalue"},"oi2":5,"1":"","ss":"dddddf"}