数组

获取数组第一条和最后一条数据

$array = [1, 2, 3, 4, 5];
$lastElement = reset($array);
$lastElement = end($array);

从数组中提取某一列的值

$ids = array_column($items, 'id');

添加两位小数

number_format($value['amount'], 2, '.', '');

数组根据某值升序

降序只需要把$a和$b到位置调换就行,二层使用uasort

usort($list, function($a, $b) {
    return ($a['order'] ?: PHP_INT_MAX) <=> ($b['order'] ?: PHP_INT_MAX);
});

字符串

字符串转数组

$detail['business_hours'] = "["[]","[10,11,12,13]","[17,18,19,20,21]","[0,1]"]";

$business_hours = array_map(function ($item) {
  return json_decode($item, true); // 将每个子项也解码为数组
}, json_decode($detail['business_hours'], true));

筛选数组不含有测试的数据

$deviceList = array_filter($deviceList, function ($item) {
    return mb_strpos($item['name'], '测试') === false;
})

时间处理

//获取当前时间的分
date('i', time());

//时间戳
//获取当前时间的30分的时间戳
strtotime(date('Y-m-d H:30:00', time()));
//时间转秒级时间戳
strtotime($dateStr);
//获取当天最后一秒时间戳
strtotime('tomorrow - 1 second');
//获取当天指定整点小时的时间戳
strtotime("today 17:00");
//获取第二天指定整点小时的时间戳
strtotime("tomorrow 17:00");

//时间戳转时间
date("Y-m-d", $timestamp);

状态码

400 Bad Request
当请求无法被服务器理解或处理时使用。例如,请求缺少必要的参数或参数格式错误等。
401 Unauthorized
当请求需要用户身份验证时使用。如果用户未通过身份验证,则应该返回此状态码。
403 Forbidden
当请求被理解但服务器拒绝执行时使用。这通常是因为用户权限不足。
404 Forbidden
当请求不存在的数据时执行时使用
409 Conflict
当请求不能完成因为它是冲突的。例如,当试图创建一个已经存在的资源时。
422 Unprocessable Entity (WebDAV extensions)
当请求被服务器理解但是不能被处理时使用。通常用于语义错误的情况,比如数据违反了约束条件。
500 Internal Server Error
当服务器遇到一个未曾预料的情况,无法完成对请求的处理时使用。这通常意味着服务器内部出现了错误。

其他

定义一个全局文件,在application/common.php文件中定义这个

require_once dirname(__FILE__) . '/util/proComm.php';
最后修改:2025 年 08 月 18 日
如果觉得我的文章对你有用,请随意赞赏