diff --git a/src/facade/Arrays.php b/src/facade/Arrays.php index 19109fd..47a64fe 100644 --- a/src/facade/Arrays.php +++ b/src/facade/Arrays.php @@ -27,8 +27,9 @@ use think\Facade; * @package think\facade * @mixin helper * - * @method helper rand(int $num) mixed 数组随机返回一个下标 - * @method helper randValue(int $num) mixed 数组随机返回一个值 + * @method helper rand(array $arr) mixed 数组随机返回一个下标 + * @method helper randValue(array $arr) mixed 数组随机返回一个值 + * @method helper split(array $data, $num = 5) array 分隔数组 */ class Arrays extends Facade { diff --git a/src/helper/Arrays.php b/src/helper/Arrays.php index 0499cb0..41cb368 100644 --- a/src/helper/Arrays.php +++ b/src/helper/Arrays.php @@ -13,6 +13,7 @@ // | github 仓库地址 :https://github.com/GC0202/ThinkLibrary // | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library // +---------------------------------------------------------------------- +declare (strict_types=1); namespace DtApp\ThinkLibrary\helper; @@ -28,7 +29,7 @@ class Arrays * @param $arr * @return mixed */ - public function rand($arr) + public function rand(array $arr) { return array_rand($arr); } @@ -38,9 +39,30 @@ class Arrays * @param $arr * @return mixed */ - public function randValue($arr) + public function randValue(array $arr) { return $arr[array_rand($arr)]; } + /** + * 分隔数组 + * @param array $data 数组 + * @param int $num 数量 + * @return array + */ + public function split(array $data, $num = 5): array + { + $arrRet = array(); + if (!isset($data) || empty($data)) return $arrRet; + $iCount = count($data) / $num; + if (!is_int($iCount)) $iCount = ceil($iCount); + else $iCount += 1; + for ($i = 0; $i < $iCount; ++$i) { + $arrInfos = array_slice($data, $i * $num, $num); + if (empty($arrInfos)) continue; + $arrRet[] = $arrInfos; + unset($arrInfos); + } + return $arrRet; + } }