craigh/jmespath-iterator
Composer 安装命令:
composer require craigh/jmespath-iterator
包简介
Access a PHP array using Jmespath just like you would a standard array
README 文档
README
JmespathIterator allows you to access a PHP array using JMESPath just like you would a standard array.
Install
You can install JmespathIterator via composer:
composer require craigh/jmespath-iterator
Usage
Create a new JmespathIterator object, then pass the JMESPath expression as the array key:
Basic Example
use Humps\Jmespath\JmespathIterator; $iterator = new JmespathIterator([ 'foo' => [ 'bar' => [ 'baz' => 'qux', ], ], ]); echo $iterator['foo.bar.baz']; // output: 'qux'
List Projection Example
use Humps\Jmespath\JmespathIterator; $iterator = new JmespathIterator([ 'people' => [ [ 'first' => 'James', 'last' => 'd', ], [ 'first' => 'Jacob', 'last' => 'e', ], [ 'first' => 'Jayden', 'last' => 'f', ], [ 'missing' => 'different', ], ], 'foo' => [ 'bar' => 'baz', ], ]); var_dump($iterator['people[*].first']); // output: ["James", "Jacob", "Jayden"]
Every Level is a JmespathIterator
JmespathIterator always returns arrays as JmespathIterator objects, which means you can query nested arrays using JMESPath:
use Humps\Jmespath\JmespathIterator; $iterator = new JmespathIterator([ [ 'foo' => [ 'bar' => 'qux', ], ], ]); echo $iterator[0]['foo.bar']; // output: 'qux'
And iterate over it just like a standard array:
use Humps\Jmespath\JmespathIterator; $iterator = new JmespathIterator([ [ 'bar' => [ 'baz' => 'qux', ], ], [ 'bar' => [ 'baz' => 'qux', ], ], ]); if(count($iterator)){ foreach ($iterator as $value) { echo $value['bar.baz']; } }
Add Items like An Array
JmespathIterator implements the ArrayAccess interface, so you can add array values just as you usually would:
use Humps\Jmespath\JmespathIterator; $iterator = new JmespathIterator(); $iterator[] = 'foo'; $iterator[] = 'bar'; echo $iterator[1] // output: 'bar';
Slicing
To make things cleaner, you don't need to wrap slice expressions in square brackets, but you can if you want:
use Humps\Jmespath\JmespathIterator; $iterator = new JmespathIterator(['foo','bar','baz','qux', 'qux']); var_dump($iterator['0::2']); // outputs: ['foo', 'baz', 'qux'] var_dump($iterator['[0::2]']); // outputs: ['foo', 'baz', 'qux']
Remember: It's Not Actually An Array!
While a JmespathIterator object might feel like an array, it's not; but if you need it to be you can use the toArray() method:
use Humps\Jmespath\JmespathIterator; $iterator = new JmespathIterator(['foo','bar','baz','qux', 'quxx']); $array = $iterator->toArray(); natsort($array); $newIterator = new JmespathIterator($array); var_dump($newIterator); // outputs: ['bar', 'baz','foo', 'qux', 'quxx']
craigh/jmespath-iterator 适用场景与选型建议
craigh/jmespath-iterator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 02 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 craigh/jmespath-iterator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 craigh/jmespath-iterator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 23
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-02-11