Knot
, (*1)
Knot is a very powerful tool for array. Knot help for using arrays like objects! And Knot have very rich function library., (*2)
Simple Example;, (*3)
$obj = ar(1,2,3);
$obj->merge(array(3,3,4,5,6,7))->unique();
$obj->child = array("a", "b", "c");
$array = $obj->toArray();
In this example, first Knot created and Knot's data have array(1,2,3)
for now. Then $obj's data is merged with array(3,3,4,5,6,7)
. Then array_unique
function called and $obj's data changed. The next line, created child key in data and equalized to array("a", "b", "c")
. In the last line, $obj's data is converted to Array., (*4)
Knot is prepared with PSR-0 and PSR-1 standards., (*5)
Documentation
Visit documentation for more information., (*6)
Install
Knot is available for Composer., (*7)
composer require knot/knot
Or download from Github., (*8)
Use like Array
Knot object can be used like Arrays. Because Knot have ArrayAccess interface., (*9)
Example:, (*10)
$obj = ar();
$obj["new"]["way"] = "new way value";
$obj[][][][] = 1;
Knot's data can be changed with this way., (*11)
Other Using Ways
Also __get
method can be access to Knot's data. But if target is an Array, then get
return Knot Child. So functions can be used with recursive., (*12)
Also __set
functions can be changed to Knot's data., (*13)
Example:, (*14)
$obj = ar();
$obj->new = array(
"way" => "new value"
);
// Returns array("way" => "new value")
$obj->new->toArray();
This is a point to take into consideration. If trying to setting nonexistent way, then Knot's data merged with stdClass objects., (*15)
Example:, (*16)
$obj = ar();
$obj->new->way->foo = 1;
In this example, $obj's data's new
key is changed with stdClass object., (*17)
Base Functions
Knot have many functions. Also It can be added new functions to Knot., (*18)
Knot Functions
Knot have many own functions., (*19)
get($path, [$default_value])
Knot can find target data with address. So Knot can find target with small commands., (*20)
Knot using .
letter for show address., (*21)
Example Address: foo.sub.target
, (*22)
In this example's target is "sub" key of "foo" of Knot's data., (*23)
Let's get target with this way., (*24)
Example:, (*25)
$obj = arr(array(
"foo" => array(
"sub" => array(
"target" => 123
)
)
));
// Returns $obj["foo"]["sub"]["target"]
$target = $obj->get("foo.sub.target");
Also get
can be used with default value. If get
have default value and target nonexistent, then get
create target and change this value to default value., (*26)
If target is nonexistent and default value doesn't given, then get
throws an error., (*27)
In addition, if target is an array, then Knot will return Knot Child for recursive using., (*28)
set($path, $value)
set
use same thing that get
functions address usage. set
change to value from target of Knot's data., (*29)
If changed value is an array, Knot return Knot Child so, (*30)
In addition, if target is an array, then Knot will return Knot Child for recursive using., (*31)
del($path)
del
use same thing that get
functions address usage. del
delete targets., (*32)
License
The Knot is open-sourced software licensed under the MIT license., (*33)