tfhinc / ci-ray
Ray 是 Codeigniter 框架的一个富有表现力的 PHP 数组类。
Requires
- php: >=7.1.0
Requires (Dev)
- phpunit/phpunit: 4.*
This package is not auto-updated.
Last update: 2024-10-03 05:57:20 UTC
README
Ray 是 Codeigniter 框架的一个富有表现力的 PHP 数组库。
要求
- PHP >= 7.1.0
- CodeIgnitor 3.x
安装
composer require tfhinc/ci-ray
运行安装后的命令以将辅助程序和类文件发布到适当的 CI 目录
composer --working-dir=vendor/tfhinc/ci-ray/ run-script publish-files
加载库
加载 Warehouse 库有几种可用选项
使用 ray()
辅助函数
Ray 辅助函数将通过 CI 实例解析 Ray
类。它将加载类或返回现有的类实例
$this->load->helper('ray');
使用 Ray 类
当需要时可以实例化 Ray 类
$ray = new TFHInc/Ray/Ray();
使用 Ray CI 库
Ray 类可以像任何其他 CI 库一样加载
$this->load->library('Ray');
用法
Ray
可以以各种方式用于操作和转换数组。
方法使用
Ray
允许您通过返回单个值或转换后的数组的方法与数组交互
// Given an array... $fruit = [ 'lemon' => 'yellow', 'apple' => 'red', 'lime' => 'green', 'pear' => 'green', ]; // ...Get all of the keys except 'apple' and 'lime': ray($fruit)->except(['apple', 'lime'])->toArray(); /* [ 'lemon' => 'yellow', 'pear' => 'green', ] */ // ...Get the first value: ray($fruit)->first(); /* 'yellow' */ // ...Sort by value: ray($fruit)->sortByValues()->toArray(); /* [ 'lime' => 'green', 'pear' => 'green', 'apple' => 'red', 'lemon' => 'yellow', ] */
方法链
将方法链接起来以操作数组是 Ray
的强大之处
// Given a multidimensional array... $fruit_multi = [ [ 'id' => 1, 'name' => 'lemon', 'color' => 'yellow', 'price' => 2.25, 'qty' => 2 ], [ 'id' => 2, 'name' => 'apple', 'color' => 'red', 'price' => 0.99, 'qty' => 12 ], [ 'id' => 3, 'name' => 'lime', 'color' => 'green', 'price' => 3.50, 'qty' => 9 ], [ 'id' => 4, 'name' => 'pear', 'color' => 'green', 'price' => 2.00, 'qty' => 7 ], ]; // ...Group the array by the 'color' key and only return keys 'red' or 'green': ray($fruit_multi)->groupBy('color')->only(['red', 'green'])->toArray(); /* [ 'red' => [ [ 'id' => 2, 'name' => 'apple', 'color' => 'red', 'price' => 0.99, 'qty' => 12, ], ], 'green' => [ [ 'id' => 3, 'name' => 'lime', 'color' => 'green', 'price' => 3.5, 'qty' => 9, ], [ 'id' => 4, 'name' => 'pear', 'color' => 'green', 'price' => 2, 'qty' => 7, ], ], ] */ // ...Where the 'color' key is 'green', sum the 'price': ray($fruit_multi)->where('color', 'green')->sum('price'); /* 5.5 */ // ...Where the 'color' key is not 'green', filter the items that have a 'price' greater than 2: ray($fruit_multi)->whereNot('color', 'green')->filter(function($item, $key) { return $item['price'] > 2; })->toArray(); /* [ [ 'id' => 1, 'name' => 'lemon', 'color' => 'yellow', 'price' => 2.25, 'qty' => 2, ] ] */ // ...Where the 'color' key is 'green' or 'yellow', count the number of items: ray($fruit_multi)->whereIn('color', ['green','yellow'])->count(); /* 3 */ // ...Retreive a column by the 'color' key and key the transformed array by the `name` key, sort by key: ray($fruit_multi)->column('color', 'name')->sortByKeys()->toArray(); /* [ 'apple' => 'red', 'lemon' => 'yellow', 'lime' => 'green', 'pear' => 'green', ] */
方法返回类型
Ray
方法将根据方法预期的结果返回不同的数据类型。每个文档化的方法定义都指明了返回的数据类型。
- 应该在方法链序列的末尾调用
toArray()
方法以返回最终的转换后的array
// toArray() returns the final transformed array: ray($fruit_multi)->column('color', 'name')->sortByKeys()->toArray(); /* [ 'apple' => 'red', 'lemon' => 'yellow', 'lime' => 'green', 'pear' => 'green', ] */
- 返回
string
或integer
的方法不需要toArray()
方法。这些方法应该在方法链序列的末尾调用
// count() returns an integer: ray($fruit_multi)->whereIn('color', ['green','yellow'])->count(); /* 3 */ // first() returns a string: ray($fruit)->first(); /* 'yellow' */
可用方法
以下方法是当前可用的
- sortByKeys
- sortByValues
- has
- contains
- sum
- avg
- count
- values
- first
- last
- except
- only
- unique
- groupBy
- column
- where
- whereIn
- whereNot
- whereNotIn
- filter
- reduce
sortByKeys()
按键对数组进行排序。
ray($fruit)->sortByKeys()->toArray(); /* Array ( [apple] => red [lemon] => yellow [lime] => green [pear] => green ) */
sortByValues()
按值对数组进行排序。
ray($fruit)->sortByValues()->toArray(); /* Array ( [lime] => green [apple] => red [lemon] => yellow ) */
has(string $key)
确定数组是否包含给定的键。
ray($fruit_multi)->has('price'); // true ray($fruit_multi)->has('brand'); // false
contains(string $value [, string $key])
确定数组是否包含给定的值。
ray($fruit_multi)->contains('green'); // true ray($fruit_multi)->contains('brown'); // false
可选地提供键以限制 contains()
检查
ray($fruit_multi)->contains('color', 'green'); // true ray($fruit_multi)->contains('color', 'brown'); // false
sum(string $key)
获取给定键的值的总和。
ray($fruit_multi)->sum('qty'); // 30 ray($fruit_multi)->sum('price'); // 8.74
avg(string $key)
获取给定键的值的平均值。
ray($fruit_multi)->avg('price'); // 2.185
count()
获取值的计数。
ray($fruit_multi)->count(); // 4
values()
获取数组的值。可用于使用连续整数重新索引数组。
ray($fruit)->values()->toArray(); /* Array ( [0] => yellow [1] => red [2] => green [3] => green ) */
first()
获取数组的第一个值。
ray($fruit)->first(); // yellow ray($fruit_multi)->first(); /* Array ( [id] => 1 [name] => lemon [color] => yellow [price] => 2.25 [qty] => 2 ) */
last()
获取数组的最后一个值。
ray($fruit)->last(); // green ray($fruit_multi)->last(); /* Array ( [id] => 4 [name] => pear [color] => green [price] => 2 [qty] => 7 ) */
except(array $keys)
获取所有数组元素,除了提供的键。
ray($fruit)->except(['apple', 'lime'])->toArray(); /* Array ( [lemon] => yellow [pear] => green ) */
only(array $keys)
仅获取提供的键的数组元素。
ray($fruit)->only(['apple', 'lime'])->toArray(); /* Array ( [apple] => red [lime] => green ) */
unique([string $key])
通过唯一值限制数组。可选地通过提供的键的唯一值进行限制。保留数组键。如果有重复的值,则保留第一个键/值对。
ray($fruit)->unique()->toArray(); /* Array ( [lemon] => yellow [apple] => red [lime] => green ) */ ray($fruit_multi)->unique('color')->toArray(); /* Array ( [0] => Array ( [id] => 1 [name] => lemon [color] => yellow [price] => 2.25 [qty] => 2 ) [1] => Array ( [id] => 2 [name] => apple [color] => red [price] => 0.99 [qty] => 12 ) [2] => Array ( [id] => 3 [name] => lime [color] => green [price] => 3.5 [qty] => 9 ) ) */
groupBy(string $key)
根据给定的键对数组进行分组。
ray($fruit_multi)->groupBy('color')->toArray(); /* Array ( [yellow] => Array ( [0] => Array ( [id] => 1 [name] => lemon [color] => yellow [price] => 2.25 [qty] => 2 ) ) [red] => Array ( [0] => Array ( [id] => 2 [name] => apple [color] => red [price] => 0.99 [qty] => 12 ) ) [green] => Array ( [0] => Array ( [id] => 3 [name] => lime [color] => green [price] => 3.5 [qty] => 9 ) [1] => Array ( [id] => 4 [name] => pear [color] => green [price] => 2 [qty] => 7 ) ) ) */
column(string $key, [string $key_by])
从数组中检索整个列。可选地,根据提供的 key_by 参数对新转换后的数组进行键值。
ray($fruit_multi)->column('color')->toArray(); /* Array ( [0] => yellow [1] => red [2] => green [3] => green ) */ ray($fruit_multi)->column('color', 'name')->toArray(); /* Array ( [lemon] => yellow [apple] => red [lime] => green [pear] => green ) */
where(string $key, string $value)
根据特定的键和值限制数组。
ray($fruit_multi)->where('color', 'green')->toArray(); /* Array ( [2] => Array ( [id] => 3 [name] => lime [color] => green [price] => 3.5 [qty] => 9 ) [3] => Array ( [id] => 4 [name] => pear [color] => green [price] => 2 [qty] => 7 ) ) */
whereIn(string $key, array $values)
根据特定的键和值数组限制数组。
ray($fruit_multi)->whereIn('color', ['green', 'yellow'])->toArray(); /* Array ( [0] => Array ( [id] => 1 [name] => lemon [color] => yellow [price] => 2.25 [qty] => 2 ) [2] => Array ( [id] => 3 [name] => lime [color] => green [price] => 3.5 [qty] => 9 ) [3] => Array ( [id] => 4 [name] => pear [color] => green [price] => 2 [qty] => 7 ) ) */
whereNot(string $key, string $value)
根据给定的键和值限制数组。
ray($fruit_multi)->whereNot('color', 'green')->toArray(); /* Array ( [0] => Array ( [id] => 1 [name] => lemon [color] => yellow [price] => 2.25 [qty] => 2 ) [1] => Array ( [id] => 2 [name] => apple [color] => red [price] => 0.99 [qty] => 12 ) ) */
whereNotIn(string $key, array $values)
根据特定的键和值数组限制数组。
ray($fruit)->whereNotIn('color', ['green', 'yellow'])->toArray(); /* Array ( [1] => Array ( [id] => 2 [name] => apple [color] => red [price] => 0.99 [qty] => 12 ) ) */
filter(callable $callback)
通过提供的回调过滤数组。
ray($fruit_multi)->filter(function($value, $key) { return $value['price'] > 3; })->toArray(); /* Array ( [2] => Array ( [id] => 3 [name] => lime [color] => green [price] => 3.5 [qty] => 9 ) ) */ ray($fruit_multi)->filter(function($value, $key) { return $value['price'] < 3; })->toArray(); /* Array ( [0] => Array ( [id] => 1 [name] => lemon [color] => yellow [price] => 2.25 [qty] => 2 ) [1] => Array ( [id] => 2 [name] => apple [color] => red [price] => 0.99 [qty] => 12 ) [3] => Array ( [id] => 4 [name] => pear [color] => green [price] => 2 [qty] => 7 ) ) */
reduce(callable $callback)
通过回调将数组归约为一个单一值。
ray($fruit_multi)->reduce(function($carry, $value) { return $value['qty'] < 3 ? $carry + $value['qty'] : $carry; }); // 2
贡献
欢迎创建 GitHub 问题或发送包含任何错误修复的 pull 请求。请参阅 GitHub 问题跟踪器以了解需要帮助的问题。
致谢
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件。