本文目录导读:

在 PHP 中,虽然没有官方名为“数组助手”的类,但可以通过以下几种方式实现类似 Laravel 的 Arr 助手的工具:
创建自定义数组助手类
<?php
class Arr
{
/**
* 获取数组中的值(支持点语法)
*/
public static function get(array $array, $key, $default = null)
{
if (is_null($key)) {
return $array;
}
if (array_key_exists($key, $array)) {
return $array[$key];
}
if (strpos($key, '.') === false) {
return $default;
}
foreach (explode('.', $key) as $segment) {
if (is_array($array) && array_key_exists($segment, $array)) {
$array = $array[$segment];
} else {
return $default;
}
}
return $array;
}
/**
* 检查数组是否存在某个键
*/
public static function has(array $array, $key)
{
if (is_null($key)) {
return false;
}
if (array_key_exists($key, $array)) {
return true;
}
foreach (explode('.', $key) as $segment) {
if (is_array($array) && array_key_exists($segment, $array)) {
$array = $array[$segment];
} else {
return false;
}
}
return true;
}
/**
* 设置数组值(支持点语法)
*/
public static function set(array &$array, $key, $value)
{
if (is_null($key)) {
return $array = $value;
}
$keys = explode('.', $key);
while (count($keys) > 1) {
$key = array_shift($keys);
if (!isset($array[$key]) || !is_array($array[$key])) {
$array[$key] = [];
}
$array = &$array[$key];
}
$array[array_shift($keys)] = $value;
return $array;
}
/**
* 删除数组中的值(支持点语法)
*/
public static function forget(array &$array, $keys)
{
$original = &$array;
$keys = (array) $keys;
foreach ($keys as $key) {
if (array_key_exists($key, $array)) {
unset($array[$key]);
continue;
}
$parts = explode('.', $key);
$array = &$original;
while (count($parts) > 1) {
$part = array_shift($parts);
if (isset($array[$part]) && is_array($array[$part])) {
$array = &$array[$part];
} else {
continue 2;
}
}
unset($array[array_shift($parts)]);
}
}
/**
* 数组转为扁平数组(点语法)
*/
public static function flatten(array $array, $prefix = '')
{
$result = [];
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge(
$result,
self::flatten($value, $prefix . $key . '.')
);
} else {
$result[$prefix . $key] = $value;
}
}
return $result;
}
/**
* 数组分组
*/
public static function groupBy(array $array, $key)
{
$result = [];
foreach ($array as $item) {
if (is_array($item) && isset($item[$key])) {
$result[$item[$key]][] = $item;
} elseif (is_object($item) && isset($item->$key)) {
$result[$item->$key][] = $item;
}
}
return $result;
}
/**
* 随机获取一个或多个值
*/
public static function random(array $array, $number = 1)
{
if ($number === 1) {
return $array[array_rand($array)];
}
$keys = array_rand($array, $number);
$result = [];
foreach ((array) $keys as $key) {
$result[] = $array[$key];
}
return $result;
}
/**
* 数组排序
*/
public static function sort(array $array, $key, $direction = 'asc')
{
$direction = strtolower($direction) === 'asc' ? SORT_ASC : SORT_DESC;
array_multisort(
array_column($array, $key),
$direction,
SORT_REGULAR,
$array
);
return $array;
}
}
使用方式示例
$user = [
'name' => '张三',
'email' => 'zhangsan@example.com',
'profile' => [
'age' => 30,
'address' => [
'city' => '北京',
'street' => '长安街'
]
]
];
// 获取值
echo Arr::get($user, 'name'); // 张三
echo Arr::get($user, 'profile.address.city'); // 北京
echo Arr::get($user, 'no_exist', '默认值'); // 默认值
// 检查存在
var_dump(Arr::has($user, 'profile.age')); // true
var_dump(Arr::has($user, 'no_exist')); // false
// 设置值
$copy = $user;
Arr::set($copy, 'profile.email', 'test@example.com');
Arr::set($copy, 'phone', '13800138000');
// 扁平化
$flat = Arr::flatten($user);
// 结果: [
// 'name' => '张三',
// 'email' => 'zhangsan@example.com',
// 'profile.age' => 30,
// 'profile.address.city' => '北京',
// 'profile.address.street' => '长安街'
// ]
// 分组
$users = [
['name' => '张三', 'department' => '技术部'],
['name' => '李四', 'department' => '市场部'],
['name' => '王五', 'department' => '技术部']
];
$grouped = Arr::groupBy($users, 'department');
// 结果: [
// '技术部' => [ ['name' => '张三', ...], ['name' => '王五', ...] ],
// '市场部' => [ ['name' => '李四', ...] ]
// ]
// 随机获取
echo Arr::random(['a', 'b', 'c']); // 随机返回一个
// 排序
$sorted = Arr::sort($users, 'name', 'desc');
// 删除
$copy = $user;
Arr::forget($copy, 'profile.address.city');
使用第三方库
如果不想自己实现,可以使用现成的库:
// 1. Laravel 的 Arr 类 (需要安装 illuminate/support) composer require illuminate/support use Illuminate\Support\Arr; $value = Arr::get($array, 'key'); Arr::set($array, 'key', 'value'); // 2. spatie/array-functions composer require spatie/array-functions use Spatie\ArrayFunctions\ArrayFunctions; ArrayFunctions::get($array, 'key');
扩展原生 PHP 数组函数
function array_get($array, $key, $default = null) {
return Arr::get($array, $key, $default);
}
function array_set(&$array, $key, $value) {
return Arr::set($array, $key, $value);
}
// 使用
$user = [];
array_set($user, 'profile.name', '张三');
echo array_get($user, 'profile.name'); // 张三
这些方法可以帮助你更方便地操作 PHP 数组,特别是在处理嵌套数组时非常实用。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播