morrelinko/array-take
Composer 安装命令:
composer require morrelinko/array-take
包简介
Function for quick array item/items retrieval.
关键字:
README 文档
README
When dealing with arrays, retrieving elements from them can be cumbersome. Some create an extra array that stores the elements that are expected and do lots of isset checking on the original array.
For example, if you wanted to retrieve some values from forms, you may do something like this: (This is just an example)
<?php
if (isset($createData["username"])) {
$createData["username"] = $_POST["username"];
}
if (isset($createData["first_name"])) {
$createData["first_name"] = $_POST["fname"];
}
if (isset($createData["password"])) {
$createData["password"] = password_hash($_POST["username"], PASSWORD_DEFAULT);
}
$someService->createUser($createData);
Imagine if you were to process a very long form!!
Enough already! array_take provides a better way:
<?php
$createData = Morrelinko\array_take($_POST, [
"username",
"first_name" => "fname",
"password" => function($value) {
return password_hash($value, PASSWORD_DEFAULT):
}
]);
$someService->createUser($createData);
Usage
Retrieve values from a one-dimensional array Note: if a specified key is not available in the array, it is skipped and not included in the new array.
<?php
$user = [
"name" => "Laju Morrison",
"hobby" => "None",
"wedontneedthis" => "somevalue"
];
$correctedUser = Morrelinko\array_take($user, ["name", "hobby"]);
/**
* Output:
*
* [
* "name" => "Laju Morrison",
* "hobby" => "None",
* ]
*/
Renaming keys on the new array
<?php
$user = [
"name" => "Laju Morrison",
"birthday" => "1000/ask/google",
"wedontneedthis" => "somevalue"
];
$user = Morrelinko\array_take($user, ["name" => "user_name", "birthday"]);
/**
* Output:
*
* [
* "user_name" => "Laju Morrison",
* "birthday" => "1000/ask/google",
* ]
*/
Modify value via Anonymous function
<?php
$user = [
"name" => "Laju Morrison",
"password" => "123456"
];
$user = Morrelinko\array_take($user, [
"name",
"password" => function($value) {
return password_hash($value, PASSWORD_BCRYPT):
}
]);
/**
* Output:
*
* [
* "name" => "Laju Morrison",
* "password" => "$2y$12$QjSH496pcT5CEbzjD/vtVeH03tfHKFy36d4J0Ltp3lRtee9HDxY3K",
* ]
*/
If a two dimensional array is passed to it, the filter is applied to each item in the list
<?php
$users = [
["name" => "Laju Morrison", "status" => "Dreaming..."],
["name" => "John Doe", "status" => ""],
["name" => "Mary Alice", "status" => ""]
];
$users = Morrelinko\array_take($users, ["name"]);
/**
* Output:
*
* [
* ["name" => "Laju Morrison"],
* ["name" => "John Doe"],
* ["name" => "Mary Alice"]
* ]
*/
Enjoy Coding!!
Supported by http://contactlyapp.com
morrelinko/array-take 适用场景与选型建议
morrelinko/array-take 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 10 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「array」 「hash-map」 「assoc-array」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 morrelinko/array-take 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 morrelinko/array-take 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 morrelinko/array-take 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Functions for hash map (assoc array) traversal.
A set of useful PHP classes.
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.
统计信息
- 总下载量: 23
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-10-20