2021-08-28 16:11:38 +08:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-08-31 22:33:41 +08:00
|
|
|
namespace App\Helpers;
|
2021-08-28 16:11:38 +08:00
|
|
|
|
|
|
|
class ArrayHelper
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* 判断是否为关联数组
|
|
|
|
*
|
|
|
|
* @param array $items
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-09-04 19:13:25 +08:00
|
|
|
public static function isAssociativeArray(array $items): bool
|
2021-08-28 16:11:38 +08:00
|
|
|
{
|
|
|
|
$i = 0;
|
|
|
|
foreach (array_keys($items) as $value) {
|
|
|
|
if (!is_int($value) || $value !== $i) return true;
|
|
|
|
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-04 19:13:25 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 二维数组排序
|
|
|
|
*
|
|
|
|
* @param array $array 数组
|
|
|
|
* @param string $field 排序字段
|
|
|
|
* @param int $sort 排序方式
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function sort(array $array, string $field, $sort = SORT_DESC): array
|
|
|
|
{
|
|
|
|
array_multisort(array_column($array, $field), $sort, $array);
|
|
|
|
return $array;
|
|
|
|
}
|
2021-08-28 16:11:38 +08:00
|
|
|
}
|