hyperf-chat/app/Helper/ArrayHelper.php

40 lines
824 B
PHP
Raw Normal View History

2021-08-28 16:11:38 +08:00
<?php
declare(strict_types=1);
2022-01-22 20:08:19 +08:00
namespace App\Helper;
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
*/
2022-01-22 20:08:19 +08:00
public static function sort(array $array, string $field, int $sort = SORT_DESC): array
2021-09-04 19:13:25 +08:00
{
array_multisort(array_column($array, $field), $sort, $array);
return $array;
}
2021-08-28 16:11:38 +08:00
}