PHP array_filter() 函数详解:按条件过滤数组元素
- 2026年7月3日
- 134 次
文章目录
处理数组时,经常需要从一堆数据里挑出符合条件的。以前用 for+if 循环过滤,写得又长又丑。后来发现array_filter() 函数简直是为这个场景量身定做的。
这篇文章把array_filter()的各种用法、回调函数写法、常见坑都整理出来。
array_filter()基本用法
最基本的用法:不传回调函数,默认过滤掉所有"假值"(false、0、null、空字符串、空数组等)。
<?php
$data = [
'name' => '张三',
'email' => '',
'phone' => '13800138000',
'age' => 0,
'city' => '北京',
'address' => null,
];
// 默认过滤假值
$filtered = array_filter($data);
print_r($filtered);
/*
输出:
---
Array
(
[name] => 张三
[phone] => 13800138000
[city] => 北京
)
*/
?>
使用自定义回调函数
传入一个回调函数,只有回调返回 true 的元素才会保留。
<?php
$numbers = [1, -2, 3, -4, 5, -6, 0, 7];
// 过滤出正数
$positive = array_filter($numbers, function($n) {
return $n > 0;
});
print_r($positive);
// Array([0] => 1, [2] => 3, [4] => 5, [6] => 7)
?>
使用键名和键值双参数回调
回调函数可以接收两个参数:键名和键值。这样可以更灵活地过滤。
<?php
$user_data = [
'id' => 1001,
'name' => '张三',
'email' => 'zhangsan@example.com',
'password' => 'secret123',
'role' => 'admin',
];
// 过滤掉敏感字段(密码)
$visible = array_filter($user_data, function($key, $value) {
return $key !== 'password';
}, ARRAY_FILTER_USE_BOTH);
print_r($visible);
// 没有 password 字段了
?>
注意:第三个参数必须传 ARRAY_FILTER_USE_BOTH 才能拿到键名。不传的话回调只能拿到值,拿不到键名。
常见实战场景
场景1:清理用户提交表单数据
过滤掉空的表单字段:
<?php
// 模拟$_POST数据
$form = [
'name' => '张三',
'email' => 'zhangsan@example.com',
'phone' => '', // 用户没填
'address' => '', // 用户没填
'city' => '北京',
'zip' => null, // 用户没填
];
// 去掉空值
$clean = array_filter($form);
print_r($clean);
/*
Array
(
[name] => 张三
[email] => zhangsan@example.com
[city] => 北京
)
*/
?>
场景2:从数据库结果中筛选有效记录
<?php
// 假设从数据库查询出所有记录(包括已删除的)
$records = [
['id' => 1, 'name' => '商品A', 'status' => 'active'],
['id' => 2, 'name' => '商品B', 'status' => 'deleted'],
['id' => 3, 'name' => '商品C', 'status' => 'active'],
['id' => 4, 'name' => '商品D', 'status' => 'deleted'],
];
// 只保留 active 的记录
$active = array_filter($records, function($record) {
return $record['status'] === 'active';
});
$count = count($active);
echo "有 $count 个活跃商品\n";
?>
场景3:过滤空字符串但保留 0
<?php
// 假设从数据库查询出所有记录(包括已删除的)
$records = [
['id' => 1, 'name' => '商品A', 'status' => 'active'],
['id' => 2, 'name' => '商品B', 'status' => 'deleted'],
['id' => 3, 'name' => '商品C', 'status' => 'active'],
['id' => 4, 'name' => '商品D', 'status' => 'deleted'],
];
// 只保留 active 的记录
$active = array_filter($records, function($record) {
return $record['status'] === 'active';
});
$count = count($active);
echo "有 $count 个活跃商品\n";
?>
默认 array_filter 会把 0 也过滤掉。如果需要保留 0 但过滤空字符串:
<?php
$data = [
'count' => 0, // 计数为0,有效
'name' => '', // 空字符串,无效
'email' => 'test@test.com',
'age' => null, // null,无效
];
// 自定义过滤:只去掉空字符串和null,保留0
$filtered = array_filter($data, function($v) {
return $v !== '' && $v !== null;
});
print_r($filtered);
/*
Array
(
[count] => 0
[email] => test@test.com
)
*/
?>
场景4:过滤非数字键
<?php
$mixed = [
0 => 'zero',
1 => 'one',
'a' => 'letter_a',
2 => 'two',
'b' => 'letter_b',
];
// 只保留数字键
$numeric = array_filter($mixed, function($key) {
return is_int($key);
}, ARRAY_FILTER_USE_KEY);
print_r($numeric);
// 只有 0, 1, 2 键
?>
<?php
$mixed = [
0 => 'zero',
1 => 'one',
'a' => 'letter_a',
2 => 'two',
'b' => 'letter_b',
];
// 只保留数字键
$numeric = array_filter($mixed, function($key) {
return is_int($key);
}, ARRAY_FILTER_USE_KEY);
print_r($numeric);
// 只有 0, 1, 2 键
?>
若非作者标注皆为李涛自留地原创文章,转载或复制请以超链接形式并注明出处。
- PHP array_combine() 函数详解:用两个数组构造键值对 2026-07-02
- PHP合并数组函数array_combine()与array_merge()用法与区别 2022-10-27
- PHP文件指针函数有哪些?PHP文件指针函数怎么使用? 2022-11-14
- PHP array_multisort() 函数详解:多维数组排序的 N 种姿势 2026-07-03
- 使用Css3的transition和transform属性实现图片动态翻转效果实例 2022-05-06
- PHP如何更改imagecreatetruecolor函数背景色? 2022-11-29
