在PHP开发中,处理多维数组是常见操作,尤其是从数据库、接口返回的结构化数据中,经常需要提取特定列并做格式化处理,比如把用户列表中的id和姓名提取出来拼接成指定格式的字符串,或者把商品数组中的价格字段统一做精度处理。

基础方法:使用array_column提取特定列
PHP内置的array_column函数可以直接从多维数组中提取指定键名的列,不需要手动遍历数组,是最高效的基础提取方式。该函数第一个参数是原始多维数组,第二个参数是要提取的键名,第三个可选参数是作为返回数组索引的键名。
假设我们有一个用户数据数组,结构如下:
<?php
$users = [
['id' => 1, 'name' => '张三', 'age' => 22, 'score' => 88.5],
['id' => 2, 'name' => '李四', 'age' => 25, 'score' => 91.0],
['id' => 3, 'name' => '王五', 'age' => 23, 'score' => 76.5],
['id' => 4, 'name' => '赵六', 'age' => 24, 'score' => 95.0],
];
?>如果只需要提取所有用户的姓名列,使用array_column只需要一行代码:
<?php $names = array_column($users, 'name'); print_r($names); // 输出结果: // Array // ( // [0] => 张三 // [1] => 李四 // [2] => 王五 // [3] => 赵六 // ) ?>
结合array_map做格式化处理
提取到特定列之后,如果需要做格式化,比如把分数保留一位小数,或者拼接成指定格式的字符串,可以结合array_map函数处理。array_map可以对数组中的每个元素应用回调函数,返回处理后的新数组。
比如我们需要提取用户的id和姓名,拼接成"id:姓名"的格式,同时把分数保留一位小数,可以这么做:
<?php
// 提取id和姓名拼接,同时处理分数格式
$formattedData = array_map(function($item) {
return [
'id_name' => $item['id'] . ':' . $item['name'],
'score' => number_format($item['score'], 1)
];
}, $users);
print_r($formattedData);
// 输出结果:
// Array
// (
// [0] => Array
// (
// [id_name] => 1:张三
// [score] => 88.5
// )
// [1] => Array
// (
// [id_name] => 2:李四
// [score] => 91.0
// )
// [2] => Array
// (
// [id_name] => 3:王五
// [score] => 76.5
// )
// [3] => Array
// (
// [id_name] => 4:赵六
// [score] => 95.0
// )
// )
?>复杂场景:嵌套多维数组的提取
如果多维数组是嵌套结构,比如每个用户下还有地址子数组,需要提取子数组中的特定字段,array_column本身不支持深层提取,这时候可以先用array_map把深层数据提取到第一层,再使用array_column。
假设用户数组结构如下:
<?php
$users = [
[
'id' => 1,
'name' => '张三',
'address' => ['city' => '北京', 'street' => '朝阳路']
],
[
'id' => 2,
'name' => '李四',
'address' => ['city' => '上海', 'street' => '南京路']
],
];
?>如果需要提取所有用户的所在城市,可以先处理数组把城市放到第一层:
<?php
// 先把嵌套的city提取到第一层
$processedUsers = array_map(function($item) {
$item['city'] = $item['address']['city'];
return $item;
}, $users);
// 再提取city列
$cities = array_column($processedUsers, 'city');
print_r($cities);
// 输出:Array ( [0] => 北京 [1] => 上海 )
?>性能对比与注意事项
对比手动循环遍历和内置函数的方式,内置函数的执行效率更高,尤其是在数据量超过1000条的时候,差距会非常明显。因为array_column和array_map是PHP底层用C语言实现的,比用户层的循环逻辑执行速度快很多。
需要注意的几点:
array_column的第二个参数如果是null,会返回整个子数组,第三个参数作为索引键名时,需要保证该键名在子数组中唯一,否则后面的元素会覆盖前面的。array_map的回调函数如果不需要返回值,会返回包含null的数组,所以格式化时一定要返回处理后的数据。- 如果原始数组的键名不规范,比如有的子数组缺少要提取的键,
array_column会返回null,这时候可以在回调函数中做默认值处理。
完整示例:提取并格式化输出为JSON
实际开发中经常需要把处理后的数据返回给前端,比如输出为JSON格式,下面是一个完整的示例:
<?php
$users = [
['id' => 1, 'name' => '张三', 'age' => 22, 'score' => 88.5],
['id' => 2, 'name' => '李四', 'age' => 25, 'score' => 91.0],
['id' => 3, 'name' => '王五', 'age' => 23, 'score' => 76.5],
];
// 提取id、姓名,格式化分数,拼接显示文本
$result = array_map(function($item) {
return [
'user_id' => $item['id'],
'user_name' => $item['name'],
'formatted_score' => number_format($item['score'], 1),
'display_text' => '用户' . $item['name'] . '的分数是' . number_format($item['score'], 1)
];
}, $users);
// 设置响应头为JSON格式
header('Content-Type: application/json; charset=utf-8');
// 输出JSON
echo json_encode($result, JSON_UNESCAPED_UNICODE);
// 输出结果:
// [
// {"user_id":1,"user_name":"张三","formatted_score":"88.5","display_text":"用户张三的分数是88.5"},
// {"user_id":2,"user_name":"李四","formatted_score":"91.0","display_text":"用户李四的分数是91.0"},
// {"user_id":3,"user_name":"王五","formatted_score":"76.5","display_text":"用户王五的分数是76.5"}
// ]
?>通过上述方法,可以快速高效地完成多维数组特定列提取和格式化输出的需求,避免冗余的循环代码,提升开发效率和代码质量。
PHP多维数组array_columnarray_map数据格式化修改时间:2026-06-06 15:10:34