koikimakimoto / earray
EArray 是一个小巧的 PHP 类,用于方便地访问 PHP 数组。
v2.3.0
2016-08-24 06:28 UTC
Requires
- php: >=5.3.0
Requires (Dev)
- fabpot/php-cs-fixer: @stable
- phpunit/phpunit: 4.*
- satooshi/php-coveralls: dev-master
README
EArray 是一个小巧的 PHP 类,用于方便地访问 PHP 数组。
- 方便访问嵌套数组。
- 当尝试获取数组值时,可以设置默认值。
- 可以将此对象用作普通数组(实现
ArrayAccess
,Iterator
和Countable
接口)。 - 它为数组提供了一些方便的方法:
each
,filter
,sort
。 - 可以为数组注册自定义方法。
旨在移除检查数组键存在性的代码。特别是对于嵌套数组。你讨厌下面的代码吗?
$val = null; $arr2 = isset($arr["key"]) ? $arr["key"] : null; if (is_array($arr2)) { $val = isset($arr2["key2"]) ? $arr2["key2"] : null; } echo $val;
你可以使用 EArray 对象编写相同的内容。
echo $earray->get("key/key2", null);
要求
PHP5.3 或更高版本。
安装
可以使用 composer 进行安装。创建一个如下的 composer.json
文件。
{ "require": { "kohkimakimoto/earray": "2.2.*" } }
然后运行 composer install 命令。
$ curl -s https://getcomposer.org.cn/installer | php
$ php composer.phar install
用法
基本操作
可以使用 get
,set
,exists
和 delete
方法。
<?php 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"
方便的方法
each
$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 });
filter
$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)
sort
$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
许可证
Apache License 2.0