yii2mod/yii2-helpers
Composer 安装命令:
composer require yii2mod/yii2-helpers
包简介
Collection of useful helper functions for Yii Framework 2.0
README 文档
README
This extension is a collection of useful helper functions for Yii Framework 2.0.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist yii2mod/yii2-helpers "*"
or add
"yii2mod/yii2-helpers": "*"
to the require section of your composer.json file.
Available Methods
- add()
- average()
- collapse()
- except()
- has()
- first()
- flatten()
- last()
- only()
- pluck()
- prepend()
- pull()
- set()
- sort()
- sortRecursive()
- where()
- xmlStrToArray()
Method Listing
#####add()
Add a given key / value pair to the array if the given key doesn't already exist in the array:
$array = ArrayHelper::add(['card' => 'Visa'], 'price', 200); // ['card' => 'Visa', 'price' => 200]
#####average()
Get the average value of a given key:
ArrayHelper::average([1, 2, 3, 4, 5]); // 3
You may also pass a key to the average method:
$array = [ ['score' => 10], ['score' => 30], ['score' => 50], ]; ArrayHelper::average($array, 'score'); // 30
#####collapse()
Collapse an array of arrays into a single array:
$array = ArrayHelper::collapse([[1, 2, 3], [4, 5, 6]]); // [1, 2, 3, 4, 5, 6]
#####except()
Get all of the given array except for a specified array of items:
$array = ['name' => 'Desk', 'price' => 100]; $array = ArrayHelper::except($array, ['price']); // ['name' => 'Desk']
#####has()
Check if an item exists in an array using "dot" notation:
$array = ['products' => ['desk' => ['price' => 100]]]; $hasDesk = ArrayHelper::has($array, 'products.desk'); // true
#####first()
Return the first element in an array passing a given truth test:
$array = [100, 200, 300]; $value = ArrayHelper::first($array); // 100 // or apply custom callback $value = ArrayHelper::first($array, function($key, $value) { return $value >= 150; // 200 });
#####flatten()
Flatten a multi-dimensional array into a single level:
$array = ['name' => 'Bob', 'languages' => ['PHP', 'Python']]; $array = ArrayHelper::flatten($array); // ['Bob', 'PHP', 'Python'];
#####last()
Return the last element in an array passing a given truth test:
$array = [100, 200, 300]; $value = ArrayHelper::last($array); // 300 // or apply custom callback $value = ArrayHelper::last($array, function($key, $value) { return $value; // 300 });
#####only()
Get a subset of the items from the given array:
$array = ['name' => 'Desk', 'price' => 100, 'orders' => 10]; $array = ArrayHelper::only($array, ['name', 'price']); // ['name' => 'Desk', 'price' => 100]
#####prepend()
Push an item onto the beginning of an array:
$array = ['one', 'two', 'three', 'four']; $array = ArrayHelper::prepend($array, 'zero'); // $array: ['zero', 'one', 'two', 'three', 'four']
#####pluck()
The function retrieves all of the collection values for a given key:
$array = [ ['product_id' => 'prod-100', 'name' => 'Desk'], ['product_id' => 'prod-200', 'name' => 'Chair'], ]; $plucked = ArrayHelper::pluck($array, 'name'); // ['Desk', 'Chair']
You may also specify how you wish the resulting collection to be keyed:
$plucked = ArrayHelper::pluck($array, 'name', 'product_id'); // ['prod-100' => 'Desk', 'prod-200' => 'Chair']
#####pull()
Get a value from the array, and remove it:
$array = ['name' => 'Desk', 'price' => 100]; $name = ArrayHelper::pull($array, 'name'); // $name: Desk // $array: ['price' => 100]
#####set()
Set an array item to a given value using "dot" notation:
$array = ['products' => ['desk' => ['price' => 100]]]; ArrayHelper::set($array, 'products.desk.price', 200); // ['products' => ['desk' => ['price' => 200]]]
#####sort()
Sort the array using the given callback:
$array = [ ['name' => 'Desk'], ['name' => 'Chair'], ]; $array = ArrayHelper::sort($array, function ($value) { return $value['name']; }); /* [ ['name' => 'Chair'], ['name' => 'Desk'], ] */
#####sortRecursive()
Recursively sort an array by keys and values:
$array = [ [ 'Desc', 'Chair', ], [ 'PHP', 'Ruby', 'JavaScript', ], ]; $array = ArrayHelper::sortRecursive($array); /* [ [ 'Chair', 'Desc', ], [ 'JavaScript', 'PHP', 'Ruby', ] ]; */
#####where()
Filter the array using the given Closure.:
$array = ["100", 200, "300"]; $value = ArrayHelper::where($array, function($key, $value) { return is_string($value); }); // Will be return Array ( [0] => 100 [2] => 300 );
#####xmlStrToArray()
Convert xml string to array.
$xml = '<?xml version="1.0"?> <root> <id>1</id> <name>Bob</name> </root>'; ArrayHelper::xmlStrToArray($xml) // ['id' => 1, 'name' => 'Bob']
####StringHelper
StringHelper::removeStopWords('some text');- remove stop words from stringStringHelper::removePunctuationSymbols('some text');- Remove punctuation symbols from string
yii2mod/yii2-helpers 适用场景与选型建议
yii2mod/yii2-helpers 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 182.36k 次下载、GitHub Stars 达 15, 最近一次更新时间为 2015 年 09 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「yii2」 「yii2 helpers」 「yii2 array helpers」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yii2mod/yii2-helpers 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yii2mod/yii2-helpers 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yii2mod/yii2-helpers 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A set of useful PHP classes.
A custom URL rule class for Yii 2 which allows to create translated URL rules
Trait providing methods to set class properties with an array.
Traits to build collections of specific objects
This adds functions about array. If you feel like there few php built-in functions about array, this will be useful.
Convert array or camel case to underscore, or underscore to others.
统计信息
- 总下载量: 182.36k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 16
- 点击次数: 22
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-09-02