solidworx / util
0.1.0
2017-05-10 07:25 UTC
Requires
- php: >=7.1.0
- symfony/property-access: ^2.7|^3.0
Requires (Dev)
- phpunit/phpunit: ^6.0
This package is not auto-updated.
Last update: 2022-02-01 13:06:27 UTC
README
已弃用 此包不再维护。请使用 solidworx/lodash-php 替代。
一组日常使用的小工具类
目录
要求
最低PHP要求是PHP 7.1+
安装
Composer
$ composer require solidworx/util
用法
ArrayUtil
column:
从输入数组中返回单个列的值。输入可以是数组的数组或对象。此方法是正常 array_column
函数的增强。如果您需要从集合中获取特定值,这可能很有用。
使用数组输入
<?php $input = [ ['test' => 'one'], ['test' => 'two'], ['test' => 'three'], ]; $columns = ArrayUtil::column($input, 'test'); /* $columns = array ( 0 => 'one', 1 => 'two', 2 => 'three', );*/
使用具有公共属性的对象
<?php class Foo { public $test; } $foo1 = new Foo; $foo1->test = 'one'; $foo2 = new Foo; $foo2->test = 'two'; $foo3 = new Foo; $foo3->test = 'three'; $input = [ $foo1, $foo2, $foo3, ]; $columns = ArrayUtil::column($input, 'test'); /* $columns = array ( 0 => 'one', 1 => 'two', 2 => 'three', );*/
使用具有方法的对象
<?php class Foo { private $value; public function __construct($value) { $this->value = $value; } public function test() { return $this->>value; } } $input = [ new Foo('one'), new Foo('two'), new Foo('three'), ]; $columns = ArrayUtil::column($input, 'test'); /* $columns = array ( 0 => 'one', 1 => 'two', 2 => 'three', );*/
使用具有getter的对象
<?php class Foo { private $value; public function __construct($value) { $this->value = $value; } public function getTest() { return $this->value; } } $input = [ new Foo('one'), new Foo('two'), new Foo('three'), ]; $columns = ArrayUtil::column($input, 'test'); /* $columns = array ( 0 => 'one', 1 => 'two', 2 => 'three', );*/
获取所有用户的电子邮件地址
<?php $users = $userRepository->findAll(); $emails = ArrayUtil::column($users, 'email');
默认情况下,所有 null
值都会被过滤掉。如果您想保留 null
值,请将 false
作为第三个参数传递
$users = $userRepository->findAll(); $emails = ArrayUtil::column($users, 'email', false); // Will keep empty values in the result
测试
要运行单元测试,请执行以下命令
$ vendor/bin/phpunit
贡献
请参阅 CONTRIBUTING
许可
此库是开源软件,许可协议为 MIT 许可协议
请参阅 LICENSE 文件以获取完整的许可协议。