carlosv2/map
Composer 安装命令:
composer require carlosv2/map
包简介
Enhance PHP arrays to allow any offset type
README 文档
README
Map allows you to use an array-like object which allows keys to be of any type as opposed to the default integer or string types.
Installation
Install with:
$ composer require carlosv2/map
Usage
To use Map you only need to create an instance of it and start using it as if it was an regular array:
$foo = new Foo();
$map = new Map();
$map[$foo] = 'bar';
You can use any type you want as key. If you want, you can even mix them:
$map = new Map();
$map[null] = 'null';
$map[[]] = 'array';
$map['string'] = 'string';
$map[1] = 'integer';
$map[true] = 'boolean';
$map[new \stdClass()] = 'object';
foreach ($map as $key => $value) {
var_dump($key, $value);
}
API
The following functions are available in the Map class.
has:
Checks whether a key exists or not:
$obj = new \stdClass();
$map = new Map();
$map[$obj] = 'value';
$map->has($obj); // true
$map->has([]); // false
get:
Returns the value associated with the given key:
$obj = new \stdClass();
$map = new Map();
$map[$obj] = 'value';
$map->get($obj); // 'value'
$map->get([]); // null
Optionally, a fallback value can be returned if the given key does not exist:
$map = new Map();
$map->get([], 'default'); // 'default'
set:
Assigns the given value to the given key:
$map = new Map();
$map->set($key, $value); // Equivalent to: $map[$key] = $value;
keys:
Returns the array of keys for that instance:
$map = new Map();
$map[null] = 'null';
$map[[]] = 'array';
$map[true] = 'boolean';
$map->keys(); // [null, [], true]
values:
Returns the array of values for that instance:
$map = new Map();
$map[null] = 'null';
$map[[]] = 'array';
$map[true] = 'boolean';
$map->values(); // ['null', 'array', 'boolean']
map:
Similar to array_map but it returns another Map instance instead. The callable function may have the key as a
second argument:
$map = new Map();
$map[null] = 'null';
$map[[]] = 'array';
$map[true] = 'boolean';
$newMap = $map->map(function ($value, $key) { return json_encode($key) . '_' . $value; });
$newMap->keys(); // [null, [], true]
$newMap->values(); // ['null_null', '[]_array', 'true_boolean']
Interfaces
The Map class implements the following interfaces:
统计信息
- 总下载量: 39
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-05-26