morrelinko/array-take 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

morrelinko/array-take

最新稳定版本:1.1.0

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

统计信息

  • 总下载量: 23
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-10-20