berdidajohnlouise / array-helper
PHP中使用数组的便捷方式。
dev-main
2023-07-04 13:20 UTC
Requires (Dev)
- phpunit/phpunit: 8.5.x-dev
This package is auto-updated.
Last update: 2024-09-04 15:59:14 UTC
README
ArrayHelper - 简化PHP中的数组操作。
ArrayHelper是一个强大的PHP包,提供一系列函数以简化常见的数组操作,使数组操作变得简单。
使用ArrayHelper,您可以执行各种任务,如排序数组、合并数组、通过键检索值、检查数组是否存在或为空以及操作数组结构。
功能
- sort() - 根据属性或值轻松对数组进行升序或降序排序。
- merge() - 将多个数组合并为一个数组,合并它们的元素。
- get() - 使用键或复杂路径从数组中检索值。
- has() - 确定数组是否包含特定的键或值。
- 支持关联数组和索引数组:Arrayhelper可以无缝地与关联数组和索引数组一起工作。
安装
您可以通过Composer安装ArrayHelper包。在您的终端中运行以下命令
$ composer require berdidajohnlouise/array-helper
用法
ArrayHelper sort()
- 根据属性或值轻松对数组进行升序或降序排序。
use Berdidajohnlouise\ArrayHelper\ArrayHelper; /** * Method sorting arrays by associative or indexed array * * @param string $property => string | null * @param bool $isAscending => true | false | default value => true * * @return array ascending || descending */ // Sorting Indexed Arrays. $array = [5,3,1,4,2]; $arrayHelper = new ArrayHelper($array); // Sort Ascending $sortedIndexArray = $arrayHelper->sort(); // Output: [1,2,3,4,5] // Sort Descending $sortedIndexArray = $arrayHelper->sort(null,false); // Output: [5,4,3,2,1] // Sorting Via Associative Array $array = [ ['name' => 'John', 'age' => 30], ['name' => 'Jane', 'age' => 25], ['name' => 'Bob', 'age' => 35], ]; $arrayHelper = new ArrayHelper($array); // Sort Associative array order by ascending default $sortedArrayAsc = $arrayHelper->sort('age'); // Output = [ // ['name' => 'Jane', 'age' => 25], // ['name' => 'John', 'age' => 30], // ['name' => 'Bob', 'age' => 35], // ] // Sort Associative array order by descending $sortedArrayAsc = $arrayHelper->sort('age',false); // Output = [ // ['name' => 'Bob', 'age' => 35], // ['name' => 'John', 'age' => 30], // ['name' => 'Jane', 'age' => 25], // ]
Array merge()
- 将多个数组合并为一个数组,合并它们的元素。
use Berdidajohnlouise\ArrayHelper\ArrayHelper; /** * Method merge arrays helper function * * @param ...$arrays $arrays [can accept multiple param arrays] * * @return array */ $array1 = ['name' => 'John', 'age' => 30]; $array2 = ['city' => 'New York', 'country' => 'USA']; $array3 = ['occupation' => 'Developer']; // Without instantiating of array in constructor $arrayHelpers = new ArrayHelper; $mergedArray = $arrayHelpers->merge($array1, $array2, $array3); // Output = [ // 'name' => 'John', // 'age' => 30, // 'city' => 'New York', // 'country' => 'USA', // 'occupation' => 'Developer', // ] // instantiating of array in constructor $arrayHelpers = new ArrayHelper($array1); $mergedArray = $arrayHelpers->merge($array2, $array3); // Output = [ // 'name' => 'John', // 'age' => 30, // 'city' => 'New York', // 'country' => 'USA', // 'occupation' => 'Developer', // ]
Array get()
- 使用键或复杂路径从数组中检索值。
use Berdidajohnlouise\ArrayHelper\ArrayHelper; /** * Method get for retrieving a value from an array using a dot-separated key or direct array key. * * @param string $key => dot separated or direct array key * * @return void */ $array = ['user' => ['name' => 'Jane', 'email' => 'jane@example.com']]; $arrayHelper = new ArrayHelper($array); $value = $arrayHelper->get('user.name'); // Output: Jane OR $array = ['name' => 'John', 'age' => 30, 'city' => 'New York']; $arrayHelper = new ArrayHelper($array); $value = $arrayHelper->get('name'); // Output: John
Array has()
- 确定数组是否包含特定的键或值。
use Berdidajohnlouise\ArrayHelper\ArrayHelper; /** * Method has that checks an array if has a certain key. * * @param $key $key * * @return bool */ $array = ['name' => 'John', 'age' => 30, 'city' => 'New York']; $arrayHelpers = new ArrayHelper($array); $arrayHelpers->has('name'); // Output: True $arrayHelpers->has('gender'); // Output: False
贡献
欢迎贡献!如果您发现错误或想添加新功能,请提交GitHub仓库上的问题或拉取请求。
鸣谢
ArrayHelper由Berdida John Louise开发和维护。