数组

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

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

从数组中提取某一列的值

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

添加两位小数

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

数组内容替换

//原数据
$v = [10,11,12,13];

$timestamp = array_map(function($hour) use ($k) {
     $dayOfYear = ($k == 3 && $hour <= 5) ? 'tomorrow' : 'today';
     return strtotime("today {$hour}:00");
}, $v);

//处理后
[1751508000,1751511600,1751515200,1751518800];

数组根据某值排序

降序只需要把$a和$b到位置调换就行

//一层数组排序
usort($list, function($a, $b) {
    return ($a['order'] ?: PHP_INT_MAX) <=> ($b['order'] ?: PHP_INT_MAX);
});
//二层数组排序
uasort($list, function($a, $b) {
    return ($a['order'] ?: PHP_INT_MAX) <=> ($b['order'] ?: PHP_INT_MAX);
});

判断两个数组之间有没有相同的值

$array1 = [1, 2, 3, 4, 5];
$array2 = [3, 4, 5, 6, 7];
$common = array_intersect($array1, $array2);

if (!empty($common)) 或 if (count($common) > 0) 判断

字符串

字符串转数组

转数组

$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分的时间戳
$bbb = strtotime(date('Y-m-d H:30:00', time()));
//获取当天时间戳
strtotime(date('Y-m-d'));
//时间转秒级时间戳
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
当服务器遇到一个未曾预料的情况,无法完成对请求的处理时使用。这通常意味着服务器内部出现了错误。
最后修改:2025 年 07 月 03 日
如果觉得我的文章对你有用,请随意赞赏