binary-cube/dot-array
Composer 安装命令:
composer require binary-cube/dot-array
包简介
PHP Dot-Array :: Sail through array using the dot notation
README 文档
README
~ Enjoy your ☕ ~
Accessing PHP Arrays via DOT notation is easy as:
DotArray::create(['config' => ['some.dotted.key' => 'value']])->get('config.{some.dotted.key}')
Installing
-
via "composer require":
composer require binary-cube/dot-array
-
via composer (manually):
If you're using Composer to manage dependencies, you can include the following in your
composer.jsonfile:{ "require": { "binary-cube/dot-array": "1.*" } }
Usage
REMEMBER: YOU NEED TO KNOW YOUR DATA
DotArray::get() can return a new instance of DotArray in case the accessed path is an array or it will return the raw data value or the default given value
-
instantiation:
-
new DotArray($array); DotArray::create($array); DotArray::createFromJson($jsonString);
-
-
get:
-
// Because the key `sci-fi & fantasy` is array the returning value it will be a new instance of DotArray. $dot('books.{sci-fi & fantasy}'); // Because the price is not an array, the result will be raw data, float in this case. $dot('books.{sci-fi & fantasy}.0.price'); // Accessing the raw array. $dot('books.{sci-fi & fantasy}')->toArray(); $dot->get('books.{sci-fi & fantasy}')->toArray(); // Accessing the last leaf and getting the raw data. $dot('books.{sci-fi & fantasy}.0.name'); $dot->get('books.{sci-fi & fantasy}.0.name'); // Giving a default value in case the requested key is not found. $dot->get('key.not.exist', 'not-found-as-string'); // Vanilla PHP. $dot('books.{sci-fi & fantasy}.0.name'); $dot['books']['sci-fi & fantasy'][0]['name'];
-
-
get :: more-complex:
-
// Using dotted key and accessing without getting confused. // Allowed tokens for keeping the names with dot(.) togethers are: '', "", [], (), {} $dot->get('config.{elastic-search}.\'v5.0\'.host') $dot->get('config.{elastic-search}."v5.0".host') $dot->get('config.{elastic-search}.[v5.0].host') $dot->get('config.{elastic-search}.(v5.0).host') $dot->get('config.{elastic-search}.{v5.0}.host')
-
-
set:
-
$dot->set('books.{sci-fi & fantasy}.0.name', 'New Name'); // Vanilla PHP. $dot['books.{sci-fi & fantasy}.0.name'] = 'New Name'; $dot['books']['sci-fi & fantasy'][0]['name'] = 'New Name';
-
-
clear (empty array <=> []):
Set the contents of a given key or keys to the given value (default is empty array).
-
$dot->clear('books.{sci-fi & fantasy}'); $dot->clear('books.{sci-fi & fantasy}', null); $dot->clear('books.{sci-fi & fantasy}.0.name', null); // Multiple keys. $dot->clear([ 'books.{sci-fi & fantasy}', 'books.{children\'s books}' ]); // Vanilla PHP. $dot['books.{sci-fi & fantasy}'] = [];
-
-
delete (unset(...)):
Delete the given key or keys.
-
$dot->delete('books.{sci-fi & fantasy}'); $dot->delete('books.{sci-fi & fantasy}.0.name'); $dot->delete(['books.{sci-fi & fantasy}.0', 'books.{children\'s books}.0']);
-
-
merge:
Merges one or more arrays into master recursively.
If each array has an element with the same string key value, the latter will overwrite the former (different from array_merge_recursive).
Recursive merging will be conducted if both arrays have an element of array type and are having the same key.
For integer-keyed elements, the elements from the latter array will be appended to the former array.-
// Example 1. $dot->merge(['key_1' => ['some_key' => 'some_value']]); // Example 2. $dot->merge( [ 'key_1' => ['some_key' => 'some_value'], ], [ 'key_2' => ['some_key' => 'some_value'], ], [ 'key_n' => ['some_key' => 'some_value'] ], );
-
-
find:
Find the first item in an array that passes the truth test, otherwise return false.
The signature of the callable must be:function ($value, $key).-
$book = $dot->get('books.{children\'s books}')->find(function ($value, $key) { return $value['price'] > 0; });
-
-
filter:
Use a callable function to filter through items.
The signature of the callable must be:function ($value, $key)-
$books = $dot->get('books.{children\'s books}')->filter(function ($value, $key) { return $value['name'] === 'Harry Potter and the Order of the Phoenix'; }); $books->toArray();
-
-
filterBy:
-
/* Allowed comparison operators: - [ =, ==, eq (equal) ] - [ ===, i (identical) ] - [ !=, ne (not equal) ] - [ !==, ni (not identical) ] - [ <, lt (less than) ] - [ >, gr (greater than) ] - [ <=, lte (less than or equal to) ] - [ =>, gte (greater than or equal to) ] - [ in, contains ] - [ not-in, not-contains ] - [ between ] - [ not-between ] */ // Example 1. $books = $dot->get('books.{children\'s books}')->filterBy('price', 'between', 5, 12); // Example 2. $books = $dot->get('books.{children\'s books}')->filterBy('price', '>', 10); // Example 3. $books = $dot->get('books.{children\'s books}')->filterBy('price', 'in', [8.5, 15.49]);
-
-
where:
-
/* The signature of the `where` call can be: - where([property, comparisonOperator, ...value]) - where(\Closure) :: The signature of the callable must be: `function ($value, $key)` Allowed comparison operators: - [ =, ==, eq (equal) ] - [ ===, i (identical) ] - [ !=, ne (not equal) ] - [ !==, ni (not identical) ] - [ <, lt (less than) ] - [ >, gr (greater than) ] - [ <=, lte (less than or equal to) ] - [ =>, gte (greater than or equal to) ] - [ in, contains ] - [ not-in, not-contains ] - [ between ] - [ not-between ] */ // Example 1. (using the signature: [property, comparisonOperator, ...value]) $books = $dot->get('books.{children\'s books}')->where(['price', 'between', 5, 12]); // Example 2. (using the signature: [property, comparisonOperator, ...value]) $books = $dot->get('books.{children\'s books}')->where(['price', '>', 10]); // Example 3. (using the signature: [property, comparisonOperator, ...value]) $books = $dot->get('books.{children\'s books}')->where(['price', 'in', [8.5, 15.49]]); // Example 4. (using the signature: \Closure) $books = $dot->get('books.{children\'s books}')->where(function ($value, $key) { return $value['name'] === 'Harry Potter and the Order of the Phoenix'; });
-
-
toArray:
Getting the internal raw array.
-
// Example 1. $dot->toArray(); // Example 2. $dot->get('books.{sci-fi & fantasy}')->toArray();
-
-
toJson:
Getting the internal raw array as JSON.
-
// Example 1. $dot->toJson(); // Example 2. $dot->get('books.{sci-fi & fantasy}')->toJson();
-
-
toFlat:
Flatten the internal array using the dot delimiter, also the keys are wrapped inside {key} (1 x curly braces).
-
$dot = DotArray::create( [ 'a' => [ 'b' => 'value', ], 'b' => [ 1, 2, 3, 'array' => [ 1, 2, 3, ] ], ] ); $dot->toFlat(); /* The output will be an array: [ '{a}.{b}' => 'value', '{b}.{0}' => 1, '{b}.{1}' => 2, '{b}.{2}' => 3, '{b}.{array}.{0}' => 1, '{b}.{array}.{1}' => 2, '{b}.{array}.{2}' => 3, ], */
-
Data Sample:
$dummyArray = [ 'books' => [ 'sci-fi & fantasy' => [ [ 'name' => 'Chronicles of Narnia Box Set', 'price' => 24.55, 'currency' => '$', 'authors' => [ [ 'name' => 'C.S. Lewis' ], ], ], [ 'name' => 'A Game of Thrones / A Clash of Kings / A Storm of Swords / A Feast of Crows / A Dance with Dragons ', 'price' => 37.97, 'currency' => '$', 'authors' => [ [ 'name' => 'George R. R. Martin' ], ], ], ], 'children\'s books' => [ [ 'name' => 'Harry Potter and the Order of the Phoenix', 'price' => 15.49, 'currency' => '$', 'authors' => [ [ 'name' => 'J. K. Rowling' ], ], ], [ 'name' => 'Harry Potter and the Cursed Child', 'price' => 8.5, 'currency' => '$', 'authors' => [ [ 'name' => 'J. K. Rowling', ], [ 'name' => 'Jack Thorne' ], ], ], ], ], ];
Bugs and feature requests
Have a bug or a feature request? Please first read the issue guidelines and search for existing and closed issues. If your problem or idea is not addressed yet, please open a new issue.
Contributing guidelines
All contributions are more than welcomed. Contributions may close an issue, fix a bug (reported or not reported), add new design blocks, improve the existing code, add new feature, and so on. In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. Read the full Code of Conduct.
Versioning
Through the development of new versions, we're going use the Semantic Versioning.
Example: 1.0.0.
- Major release: increment the first digit and reset middle and last digits to zero. Introduces major changes that might break backward compatibility. E.g. 2.0.0
- Minor release: increment the middle digit and reset last digit to zero. It would fix bugs and also add new features without breaking backward compatibility. E.g. 1.1.0
- Patch release: increment the third digit. It would fix bugs and keep backward compatibility. E.g. 1.0.1
Authors
- Banciu N. Cristian Mihai
See also the list of contributors who participated in this project.
License
This project is licensed under the MIT License - see the LICENSE file for details.
binary-cube/dot-array 适用场景与选型建议
binary-cube/dot-array 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 112.05k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2018 年 12 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「array」 「dot」 「dotarray」 「php-array」 「dot-array」 「php-array-json-path」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 binary-cube/dot-array 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 binary-cube/dot-array 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 binary-cube/dot-array 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A set of useful PHP classes.
Access array data quickly/easily using dot-notation and asterisk.
A GraphViz generator for PHP. PEAR Image_GraphViz rethought.
A PHP utility library
Trait providing methods to set class properties with an array.
Traits to build collections of specific objects
统计信息
- 总下载量: 112.05k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 35
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-12-24