p_ysk/app/functions.php

54 lines
1.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* Here is your custom functions.
*/
if (!function_exists('p')) {
/**
* 将数组 key 的命名方式转换为小写驼峰
* @param array $array 被转换的数组
* @param array $keys 要转换的 key默认所有
* @return array
*/
function p(...$p)
{
if(count($p) > 1) {
foreach ($p as $k => $v) {
print_r($v);
print_r('---');
}
}else {
print_r($p[0]);
}
die;
}
}
/**
* 多维数组去重并重新索引
* @param array $array 待处理的多维数组
* @return array 去重后并重新索引的数组
*/
function uniqueMultidimensionalArray($array, $k = 'user_id') {
$unique = [];
$seenIds = []; // 用于记录已出现的user_id
foreach ($array as $item) {
// 确保子数组包含user_id字段
if (!isset($item[$k])) {
continue; // 跳过不包含user_id的子数组可选也可抛出异常
}
$userId = $item[$k];
// 如果user_id未出现过则保留该记录
if (!in_array($userId, $seenIds)) {
$seenIds[] = $userId;
$unique[] = $item;
}
// 已出现的user_id会被自动跳过去重
}
// 重新索引数组从0开始的连续数字键
return array_values($unique);
}