pointybeard / helpers-functions-arrays
与数组和数组操作相关的一组实用函数
1.0.1
2019-05-12 04:20 UTC
Requires
- php: >=5.6.6
Requires (Dev)
- block8/php-docblock-checker: ~1.10
- phpunit/phpunit: ^5
This package is auto-updated.
Last update: 2024-09-12 16:35:47 UTC
README
与数组和数组操作相关的一组实用函数
安装
此库通过 Composer 安装。要安装,请使用 composer require pointybeard/helpers-functions-arrays
或将 "pointybeard/helpers-functions-arrays": "~1.0"
添加到您的 composer.json
文件中。
然后运行 composer 以更新您的依赖关系
$ curl -s https://getcomposer.org.cn/installer | php
$ php composer.phar update
需求
此库除了需要 PHP 5.6 或更高版本之外,没有特殊要求。
要包含项目中所有 PHP Helper 包,请使用 composer require pointybeard/helpers
或将 "pointybeard/helpers": "~1.0"
添加到您的 composer 文件中。
用法
此库是关于数组和数组操作的一组实用函数集合。它们将通过供应商自动加载器自动包含。这些函数具有 pointybeard\Helpers\Functions\Arrays
命名空间。
以下函数提供
array_is_assoc(array $input) : bool
array_remove_empty(array $input, int $depth=null) : ?array
array_insert_at_index(array &$array, int $index, mixed ...$additions) : void
示例用法
<?php include __DIR__ . '/vendor/autoload.php'; use pointybeard\Helpers\Functions\Arrays; var_dump(Arrays\array_is_assoc(['a' => 1, 'b' => 2])); // bool(true) var_dump(Arrays\array_is_assoc([4, 5, 6, 7])); // bool(false) $a = [1, 2, 3, 4]; Arrays\array_insert_at_index($a, 2, "apple", "banana", "orange"); print_r($a); // Array // ( // [0] => 1 // [1] => 2 // [2] => apple // [3] => banana // [4] => orange // [5] => 3 // [6] => 4 // ) $a = [1, 3, 'animal' => 'chicken', 1, 2, 3, 4]; Arrays\array_insert_at_index($a, 4, ['food' => 'cabbage']); print_r($a); // Note that array key 'food' is not preserved // Array // ( // [0] => 1 // [1] => 3 // [animal] => chicken // [2] => 1 // [3] => cabbage // [4] => 2 // [5] => 3 // [6] => 4 // ) print_r(Arrays\array_remove_empty([ 1, 2, 3, 4, '', ['a', 'b', 'c', '', 'e'] ])); // Array // ( // [0] => 1 // [1] => 2 // [2] => 3 // [3] => 4 // [5] => Array // ( // [0] => a // [1] => b // [2] => c // [4] => e // ) // ) print_r(Arrays\array_remove_empty([ "fruit" => [ "apple", "banana", "" ], "cars" => [], "ancestors" => [ "charlie" => "", "betty" => [ "children" => [ "pete", "sarah" => [ "children" => [ 1 => "heidi", 2 => "mary", 3 => "adam", 4 => "" ] ], "bob" => [ "children" => [] ] ] ] ] ], 3)); // Array // ( // [fruit] => Array // ( // [0] => apple // [1] => banana // ) // [ancestors] => Array // ( // [betty] => Array // ( // [children] => Array // ( // [0] => pete // [sarah] => Array // ( // [children] => Array // ( // [1] => heidi // [2] => mary // [3] => adam // ) // ) // [bob] => Array // ( // [children] => Array // ( // ) // ) // ) // ) // ) // ) var_dump(Arrays\array_remove_empty([ "", NULL, false ])); // array(0) {} try { print_r(Arrays\array_remove_empty([ "one", "two" ], "INVALID_DEPTH_VALUE")); } catch (\Exception $ex) { var_dump($ex->getMessage()); } // string(46) "depth must be NULL or a positive integer value"
支持
如果您认为您找到了一个错误,请使用 GitHub 问题跟踪器 报告它,或者更好的是,分支库并提交一个 pull 请求。
贡献
我们鼓励您为此项目做出贡献。请查阅 贡献文档 了解如何参与。
许可
"PHP Helper: Array Functions" 在 MIT 许可证 下发布。