PHP code example of kohkimakimoto / earray

1. Go to this page and download the library: Download kohkimakimoto/earray 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/ */

    

kohkimakimoto / earray example snippets


$val = null;
$arr2 = isset($arr["key"]) ? $arr["key"] : null;
if (is_array($arr2)) {
    $val = isset($arr2["key2"]) ? $arr2["key2"] : null;
}

echo $val;

echo $earray->get("key/key2", null);


use Kohkimakimoto\EArray\EArray;

$earray = new EArray(array("foo" => "bar"));
$earray->get("foo");             # "bar"
$earray->get("foo2");            # null
$earray->get("foo2", "default"); # "default"

$earray->set("foo", "bar2");
$earray->get("foo");             # "bar2"

$earray->delete("foo");
$earray->get("foo");             # null

$earray->exists("foo2")          # true
$earray->exists("foo")           # false

$earray = new EArray(
    array(
        "foo" => array(
            "foo2" => array(
                "foo3",
                "foo4",
                ),
            "foo2-1" => "foo5",
            ),
        "bar",
        "hoge",
        )
);

// You can get a value from a nested array.
$earray->get("foo/foo2-1");             # "foo5".
$earray->get("foo");                    # EArray(array("foo2" => array("foo3","foo4",),"foo2-1" => "foo5"))
$earray->get("foo")->get("foo2-1");     # "foo5".
$earray->get("foo")->toArray();         # array("foo2" => array("foo3","foo4",),"foo2-1" => "foo5")

// You can change a delimiter by the third argument.
$earray->get("foo.foo2-1", null, ".");  # "foo5"

// You can set a value to a nested array.
$earray->set("foo/foo2-1", "foo5-modify");
$earray->get("foo/foo2-1");             # "foo5-modify".

// You can delete a value from a nested array.
$earray->delete("foo/foo2-1");
$earray->get("foo/foo2-1");             # null

// You can check a value existing from a nested array.
$earray->exists("foo/foo2-1")          # false

// by the constructor's second argument.
$earray = new EArray(array("foo" => array("bar" => "value")), ".");

$earray->get("foo.bar"));    // "value"

// by the setDelimiter method.
$earray->setDelimiter("-");
$earray->get("foo-bar"));    // "value"

$earray = new EArray(
    array(
        "foo" => "aaa",
        "bar" => "bbb",
        "hoge" => "eee",
        )
);

$earray->each(function($key, $value) {
    echo $key.":".$value."\n";  // foo:aaa
                                // bar:bbb
                                // hoge:eee
});

$earray->each(function($value) {
    echo $value."\n";  // aaa
                       // bbb
                       // eee
});

$earray = new EArray(
    array(
        "kohki" => 34,
        "alice" => 12,
        "bob"   => 44,
        )
);

$arr = $earray->filter(function($key, $value){
    if ($value >= 20) {
        return true;
    } else {
        return false;
    }
})->toArray(); // array("kohki" => 34, "bob" => 44)

$array = array();
$array["f"]["details"]["weight"] = 1;
$array["f"]["details"]["position"] = 34;
$array["e"]["details"]["weight"] = 2;
$array["e"]["details"]["position"] = 33;
$array["d"]["details"]["weight"] = 3;
$array["d"]["details"]["position"] = 22;
$array["c"]["details"]["weight"] = 4;
$array["c"]["details"]["position"] = 11;
$array["b"]["details"]["weight"] = 5;
$array["b"]["details"]["position"] = 2;
$array["a"]["details"]["weight"] = 6;
$array["a"]["details"]["position"] = 1;

$earray = new EArray($array);
$earray->sortByValue(function($one, $another){

    $v1 = $one->get("details/position");
    $v2 = $another->get("details/position");

    return $v1 - $v2;

})->toArray();

// Sort by details/position
// array("a" => array(...), "b" => array(...), "c" => array(...), "d" => array(...), ...)

$earray = new EArray(
    array(
        "kohki" => 30,
        "taro" => 40,
        "masaru" => 50,
        )
);

$earray->register("getAverage", function ($earray) {
    $total = 0;
    foreach ($earray as $v) {
        $total += $v;
    }
    return $total / count($earray);
});

$earray->getAverage();  // 40

// using arguments when the method is called.
$earray->register("getAverageAndAddNumber", function ($earray, $number) {
    $total = 0;
    foreach ($earray as $v) {
        $total += $v;
    }
    $ave = $total / count($earray);
    return $ave + $number;
});

$earray->getAverageAndAddNumber(100);  // 140