PHP-5: 数组常用函数大全
1. 数组基础
1.1 概述与分类
- 数组是典型的复合类型数据,它的值是由零个或多个键值对构成
- 数组中的每一个"键值对", 称之为数组成员 或者 数组元素
- 根据键的类名,可以将数组分为二类:
| 序号 |
分类 |
描述 |
| 1 |
索引数组 |
键名默认是从 0 开始递增的正整数 |
| 2 |
关联数组 |
键名是由语义化的字符串组成 |
索引数组键名可使用数字字符串,所以,索引数组也可视为关联数组的一个子集
$goods = [0=>'A1060', 1=>'华为笔记本电脑', 2=>'MateBook X Pro', 3=>9999];
printf('<pre>%s</pre>', print_r($goods,true));
$goods = ['A1060', '华为笔记本电脑', 'MateBook X Pro', 9999];
printf('<pre>%s</pre>', print_r($goods,true));
$goods = ['A1060', 5=>'华为笔记本电脑', 3.14=>'MateBook X Pro', -88=>9999];
printf('<pre>%s</pre>', print_r($goods,true));
echo "{$goods[3.14]} --- {$goods[3]}<br>";
$goods = ['id'=>'A1060', 'name'=>'华为笔记本电脑', 'model'=>'MateBook X Pro', 'price'=>9999];
printf('<pre>%s</pre>', print_r($goods,true));
$goods = ['0'=>'A1060', '1'=>'华为笔记本电脑', '2'=>'MateBook X Pro', '3'=>9999];
printf('<pre>%s</pre>', print_r($goods,true));
echo "{$goods[1]} --- {$goods['1']}<br>";
$goods = [3=>'A1060', 'name'=>'华为笔记本电脑', 10=>'MateBook X Pro', 'price'=>9999];
printf('<pre>%s</pre>', print_r($goods,true));
$goods = ['A1060', 'name'=>'华为笔记本电脑', 'MateBook X Pro', 'price'=>9999];
printf('<pre>%s</pre>', print_r($goods,true));
1.2 定义与访问
| 序号 |
定义 |
描述 |
| 1 |
array()函数 |
传统定义方式,已淘汰或不再推荐 |
| 2 |
[...]结构 |
php5.4+支持, 语法简洁,推荐使用 |
访问遵循按键名访问的规则, 多维数组与不例外
<?php
$staff1 = array('1010', '八戒','bj@php.cn');
$staff2 = array('id'=>'1010', 'name'=>'八戒', 'email'=>'bj@php.cn');
printf('<pre>%s%s</pre>', print_r($staff1,true),print_r($staff2,true));
$staff3 = ['1010', '八戒', 'bj@php.cn'];
$staff4 =['id'=>'1010', 'name'=>'八戒', 'email'=>'bj@php.cn'];
printf('<pre>%s%s</pre>', print_r($staff3,true),print_r($staff4,true));
$staff5['id'] = '1020';
$staff5['name'] = '悟空';
$staff5['email'] = 'wk@php.cn';
printf('<pre>%s</pre>', print_r($staff5,true));
$staff6[] = '1030';
$staff6[] = '唐僧';
$staff6[] = 'ts@php.cn';
printf('<pre>%s</pre>', print_r($staff6,true));
echo "$staff6[1] 的邮箱是: $staff6[2]<br>";
echo "{$staff5['name']} 的邮箱是: {$staff5['email']}<br>";
$age = 30;
$staff7 =['id'=>'1010', 'name'=>'八戒', 'age'=>$age, 'email'=>'bj@php.cn'];
printf('<pre>%s</pre>', print_r($staff7,true));
$users = [
0=>['id'=>'101', 'name'=>'玉帝', 'age'=>88],
1=>['id'=>'102', 'name'=>'王母', 'age'=>78],
2=>['id'=>'101', 'name'=>'如来', 'age'=>68],
];
printf('<pre>%s</pre>', print_r($users,true));
printf('<pre>%s</pre>', print_r($users[2],true));
echo $users[2]['age'];
1.3 遍历
| 序号 |
方式 |
描述 |
| 1 |
逐个遍历 |
使用数组内部指针 |
| 2 |
循环遍历 |
使用foreach结构 |
数组内部指针函数:
| 序号 |
函数 |
描述 |
| 1 |
current |
获取当前元素的值 |
| 2 |
key |
获取当前元素的键名 |
| 3 |
next |
将数组中的内部指针向前移动一位 |
| 4 |
prev |
将数组的内部指针倒回一位 |
| 5 |
end |
将数组的内部指针指向最后一个单元 |
| 6 |
reset |
将数组的内部指针指向第一个单元 |
foreach遍历:
| 序号 |
语法 |
描述 |
| 1 |
foreach ($arr as $value) |
遍历"值" |
| 2 |
foreach ($arr as $key=>$value) |
遍历"键与值" |
| 3 |
foreach ($arr as list(...)) |
解构遍历 |
foreach 不支持 @ 抑制错误
<?php
$stu = ['id' => '1020', 'name' => '金莲', 'age' => 18, 'course' => 'php', 'grade' => 68];
printf('[%s] => %s<br>', key($stu), current($stu));
next($stu);
printf('[%s] => %s<br>', key($stu), current($stu));
next($stu);
printf('[%s] => %s<br>', key($stu), current($stu));
end($stu);
printf('[%s] => %s<br>', key($stu), current($stu));
prev($stu);
printf('[%s] => %s<br>', key($stu), current($stu));
reset($stu);
printf('[%s] => %s<br>', key($stu), current($stu));
echo '<hr>';
reset($stu);
while (true) {
printf('[%s] => %s<br>', key($stu), current($stu));
if (next($stu)) {
continue;
} else {
break;
}
}
echo '<hr>';
$stu = ['id' => '1020', 'name' => '金莲', 'age' => null, 'course' => 'php', 'grade' => 68];
reset($stu);
while (true) {
printf('[%s] => %s<br>', key($stu), current($stu));
if (next($stu)) {
continue;
} else {
break;
}
}
echo '<hr>';
$arr = [100, 'a', 89, true, [1, 2, 3]];
for ($i = 0; $i < count($stu); $i++) {
echo is_array($arr[$i]) ? print_r($arr[$i], true) : $arr[$i], '<br>';
}
echo '<hr>';
reset($stu);
for ($i = 0; $i < count($stu); $i++) {
printf('[%s] => %s<br>', key($stu), current($stu));
next($stu);
}
echo '<hr>';
foreach ($stu as $key => $value) {
printf('[%s] => %s<br>', $key, $value);
}
echo '<hr>';
foreach ($arr as $value) {
echo is_array($value) ? print_r($value, true) : $value, '<br>';
}
echo '<hr>';
$users = [
0 => ['id' => '101', 'name' => '玉帝', 'age' => 88],
1 => ['id' => '102', 'name' => '王母', 'age' => 78],
2 => ['id' => '101', 'name' => '如来', 'age' => 68],
];
$users = [
['id' => '101', 'name' => '玉帝', 'age' => 88],
['id' => '102', 'name' => '王母', 'age' => 78],
['id' => '101', 'name' => '如来', 'age' => 68],
];
$users = [];
$users[] = ['id' => '101', 'name' => '玉帝', 'age' => 88];
$users[] = ['id' => '102', 'name' => '王母', 'age' => 78];
$users[] = ['id' => '101', 'name' => '如来', 'age' => 68];
print_r($users);
foreach ($users as $user) {
foreach ($user as $key => $value) {
echo "$key => $value <br>";
}
echo '<hr>';
}
foreach ($users as $user) {
printf('id=%s, name=%s, age=%s<br>', $user['id'], $user['name'], $user['age']);
}
echo '<hr>';
foreach ($users as list('id' => $id, 'name' => $name, 'age' => $age)) {
printf('id=%s, name=%s, age=%s<br>', $id, $name, $age);
}
echo '<hr>';
$staffs = null;
$staffs[] = ['2020', '八戒', 29];
$staffs[] = ['2021', '悟空', 39];
$staffs[] = ['2022', '唐僧', 49];
foreach ($staffs as list($id, $name, $age)) {
printf('id=%s, name=%s, age=%s<br>', $id, $name, $age);
}
echo '<hr>';
foreach ($staffs as list($id, $name)) {
printf('id=%s, name=%s<br>', $id, $name);
}
foreach ($staffs as list($id, $name, $age, $email)) {
printf('id=%s, name=%s, email=%s<br>', $id, $name,$age, $email);
}
echo '<hr>';
$staffs = null;
$staffs[] = ['2020', '八戒', 29, [67, 88]];
$staffs[] = ['2021', '悟空', 39, [99, 31]];
$staffs[] = ['2022', '唐僧', 49, [56, 63]];
foreach ($staffs as list($id, $name, $age, list($js, $php))) {
printf('id=%s, name=%s, age=%s, js=%s, php=%s<br>', $id, $name,$age, $js, $php);
}
echo '<hr>';
$obj = new stdClass();
$obj->name = 'admin';
$obj->email = 'admin@php.cn';
$obj->role = 'custom';
foreach ($obj as $prop=>$value) {
printf('%s => %s<br>',$prop, $value);
}
2. 数组函数
2.1. 键名相关
| 序号 |
函数 |
描述 |
| 1 |
array_keys |
获取所有键名组成的数组 |
| 2 |
array_key_exists |
是否存在指定键名 |
| 3 |
array_key_last |
获取最后一个键名 php7.3+ |
| 4 |
array_key_first |
获取第一个键名 php7.3+ |
$arr = ['id' => 1, 'username' => 'admin', 'email' => 'admin@php.cn'];
print_r(array_keys($arr));
echo '<hr>';
echo (array_key_exists('email', $arr) ? '存在' : '不存在') . '<hr>';
echo array_key_last($arr) . '<hr>';
echo array_key_first($arr) . '<hr>';
2.2 与值相关
| 序号 |
函数 |
描述 |
| 1 |
array_values |
返回数组中所有值组成的数组 |
| 2 |
in_array |
检查数组中是否存在某个值 |
| 3 |
array_search |
搜索指定的值,返回键名 |
| 4 |
array_unique |
删除重复的值 |
$arr = [3 => 10, 9 => 20, 0 => 'html', 'id' => 'css', 20 => 20, 30];
print_r($arr);
echo '<hr>';
print_r(array_values($arr));
echo '<hr>';
echo (in_array('html', $arr) ? '存在' : '不存在') . '<br>';
echo array_search('css', $arr);
echo '<br>';
echo $arr[array_search('css', $arr)] . '<br>';
$newArr = array_unique($arr);
print_r($newArr);
2.3 与统计相关
| 序号 |
函数 |
描述 |
| 1 |
count |
统计元素数量或对象属性数量 |
| 2 |
array_count_values |
统计所有值的出现频率 |
$arr = [10, 3, 5, 3, 10, 5, 7, 3, 10, 7, 7];
printf('<pre>%s</pre>', print_r($arr, true));
echo '数组元素数量: ' . count($arr);
$res = array_count_values($arr);
printf('<pre>%s</pre>', print_r($res, true));
2.4 与计算相关
| 序号 |
函数 |
描述 |
| 1 |
array_sum |
对数组中所有值求和 |
| 2 |
array_product |
计算数组中所有值的乘积 |
$arr = [10, 20, 30];
echo array_sum($arr) . '<hr>';
echo array_product($arr);
2.5 栈与队列
| 序号 |
函数 |
描述 |
| 1 |
array_push |
从尾部添加一个或多个元素 |
| 2 |
array_pop |
从尾部删除最后一个元素 |
| 3 |
array_unshift |
从头部添加一个或多个元素 |
| 4 |
array_shift |
从头部删除一个元素 |
$stack = [];
echo array_push($stack, 10) . '<br>';
echo array_push($stack, 20, 30) . '<br>';
print_r($stack);
echo '<hr>';
echo array_pop($stack) . '<br>';
echo array_pop($stack) . '<br>';
echo array_pop($stack) . '<br>';
var_dump(array_pop($stack));
echo '<hr>';
$stack = [];
echo array_unshift($stack, 'one') . '<br>';
echo array_unshift($stack, 'two', 'three') . '<br>';
print_r($stack);
echo '<hr>';
echo array_shift($stack) . '<br>';
echo array_shift($stack) . '<br>';
echo array_shift($stack) . '<br>';
var_dump(array_pop($stack));
echo '<hr>';
$queue = [];
echo array_push($queue, 10, 20, 30) . '<br>';
print_r($queue);
echo '<hr>';
echo array_shift($queue) . '<br>';
echo array_shift($queue) . '<br>';
echo array_shift($queue) . '<br>';
$queue = [];
echo array_unshift($queue, 'one', 'two', 'three');
print_r($queue);
echo '<hr>';
echo array_pop($queue) . '<br>';
echo array_pop($queue) . '<br>';
echo array_pop($queue) . '<br>';
var_dump(array_pop($queue));
echo '<hr>';
2.6 排序
2.6.1 对值排序
| 序号 |
函数 |
描述 |
| 1 |
sort |
按值升序排序, 索引重排 |
| 2 |
asort |
按值升序排序, 索引保持不变 |
| 3 |
rsort |
按值降序排序, 索引重排 |
| 4 |
arsort |
按值降序排序, 索引保持不变 |
2.6.2 对键排序
| 序号 |
函数 |
描述 |
| 1 |
ksort |
按键名升序排序 |
| 2 |
krsort |
按键名降序排序 |
2.6.3 自定义排序
| 序号 |
函数 |
描述 |
| 1 |
usort |
自定义函数对值进行排序 |
| 2 |
uasort |
自定义函数对值排序并保持索引不变 |
| 3 |
uksort |
自定义函数对键名进行排序 |
2.6.4 自然排序
| 序号 |
函数 |
描述 |
| 1 |
natsort |
支持数字型字符串排序 |
| 2 |
natcasesort |
不区分大小写 |
2.6.5 乱序反转
| 序号 |
函数 |
描述 |
| 1 |
shuffle |
随机打乱一个数组的顺序 |
| 2 |
array_flip |
交换数组中的键和值 |
| 3 |
array_reverse |
反转一个数组 |
示例:
$arr = [30, 4, 82, 15, 20, 'abc', 'hello', 2, 46];
printf('原始数组:<pre>%s</pre><hr>', print_r($arr, true));
sort($arr);
printf('升序索引重置:<pre>%s</pre>', print_r($arr, true));
$arr = [30, 4, 82, 15, 20, 'abc', 'hello', 2, 46];
asort($arr);
printf('升序索引不变:<pre>%s</pre>', print_r($arr, true));
$arr = [30, 4, 82, 15, 20, 'abc', 'hello', 2, 46];
rsort($arr);
printf('降序索引重置:<pre>%s</pre>', print_r($arr, true));
$arr = [30, 4, 82, 15, 20, 'abc', 'hello', 2, 46];
arsort($arr);
printf('降序索引不变:<pre>%s</pre>', print_r($arr, true));
echo '<hr>';
$arr = ['e' => 10, 'a' => 30, 'p' => 50];
ksort($arr);
printf('按键名升序:<pre>%s</pre>', print_r($arr, true));
$arr = ['e' => 10, 'a' => 30, 'p' => 50];
krsort($arr);
printf('按键名降序:<pre>%s</pre>', print_r($arr, true));
echo '<hr>';
$arr = [90, 33, 4, 10, 2, 12];
usort($arr, function ($a, $b) {
return $a - $b;
});
printf('自定义升序:<pre>%s</pre>', print_r($arr, true));
$arr = [90, 33, 4, 10, 2, 12];
uasort($arr, function ($a, $b) {
return $a - $b;
});
printf('自定义升序且索引不变:<pre>%s</pre>', print_r($arr, true));
echo '<hr>';
$arr = ['img1.jpg', 'img5.jpg', 'img10.jpg', 'img8.jpg'];
sort($arr);
printf('普通升序:<pre>%s</pre>', print_r($arr, true));
$arr = ['img1.jpg', 'img5.jpg', 'img10.jpg', 'img8.jpg'];
natsort($arr);
printf('自然升序:<pre>%s</pre>', print_r($arr, true));
echo '<hr>';
$arr = ['id' => 109, 'username' => 'peter', 'age' => 27, 'salary' => 99999];
shuffle($arr);
printf('随机扰乱:<pre>%s</pre>', print_r($arr, true));
$arr = array_reverse($arr);
printf('翻转数组:<pre>%s</pre>', print_r($arr, true));
$arr = ['name' => 'admin', 'age' => 30, 'salary' => 8888];
$arr = ['name' => 'admin', 'age' => 30, 'salary' => false];
$arr = array_flip($arr);
printf('交换键值:<pre>%s</pre>', print_r($arr, true));
2.7 查询与替换
| 序号 |
函数 |
描述 |
| 1 |
array_slice |
从数组中取出一部分 |
| 2 |
array_splice |
去掉数组中一部分并用其它值代替 |
| 3 |
array_rand |
从数组中随机取出一个或多个元素的键名 |
| 4 |
array_column |
获取多维数组中一列组成的新数组 |
| 5 |
array_replace |
使用后面数组的值替换第一个数组的值 |
| 6 |
array_replace_recursive |
使用传递的数组递归替换第一个数组的元素 |
| 7 |
array_intersect |
计算数组的交集 |
| 8 |
array_intersect_assoc |
返回数组交集,键名也做比较 |
| 9 |
array_diff |
返回数组的差集 |
| 10 |
array_diff_assoc |
返回数组差集,键名也做比较 |
示例:
$stu = ['id' => 101, 'name' => '无忌', 'age' => 20, 'course' => 'php', 'grade' => 80];
$res = array_slice($stu, 0);
$res = array_slice($stu, 0, 2);
$res = array_slice($stu, 1, 3);
$res = array_slice($stu, -3, 2);
$res = array_slice($stu, 1, -1);
$res = array_slice(array_values($stu), 1, -1, false);
$res = array_slice(array_values($stu), 1, -1, true);
printf('<pre>%s</pre>', print_r($res, true));
echo '<hr>';
$arr = [10, 28, 9, 33, 56, 21, 82, 47];
printf('原始数组元素: <pre>%s</pre>', print_r($arr, true));
$res = array_splice($arr, -4, -2, [888, 999]);
printf('被删除的元素:<pre>%s</pre>', print_r($res, true));
printf('当前数组元素: <pre>%s</pre>', print_r($arr, true));
echo '<hr>';
$arr = ['一等奖', '二等奖', '三等奖', '谢谢参与'];
$res = array_rand($arr);
printf('%s<br>', $res);
printf('[%s] => %s<br>', $res, $arr[$res]);
$res = array_rand($arr, 3);
printf('<pre>%s</pre>', print_r($res, true));
foreach ($res as $key) {
printf('%s<br>', $arr[$key]);
}
echo '<hr>';
$arr = null;
$arr[] = ['id' => 101, 'name' => 'jack', 'age' => 20];
$arr[] = ['id' => 102, 'name' => 'mike', 'age' => 30];
$arr[] = ['id' => 103, 'name' => 'pony', 'age' => 40];
$res = array_column($arr, 'name');
$res = array_column($arr, 'name', 'id');
printf('<pre>%s</pre>', print_r($res,true));
echo '<hr>';
$arr = ['type'=>'mysql', 'host'=>'localhost', 'username'=>'root', 'password'=>'root'];
$arr1 = ['host'=>'127.0.0.1', 'username'=>'admin'];
$arr2 = ['username'=>'peter', 'password'=>'123456'];
$res =array_replace($arr, $arr1, $arr2);
printf('<pre>%s</pre>', print_r($res,true));
echo '<hr>';
$arr1 = [10, 20, 30, 40, 'php'];
$arr2 = [1,'php', 20, 30, 5];
$arr3 = ['a','c', 'php', 30, 10, 20];
$res = array_intersect($arr1, $arr2, $arr3);
printf('<pre>%s</pre>', print_r($res,true));
echo '<hr>';
$res = array_diff($arr1, $arr2, $arr3);
printf('<pre>%s</pre>', print_r($res,true));
2.8 分割与合并
| 序号 |
函数 |
描述 |
| 1 |
array_combine |
通过合并两个数组来创建一个新数组 |
| 2 |
array_merge |
把一个或多个数组合并为一个数组 |
| 3 |
array_chunk |
将一个数组分割成多个子数组 |
示例:
$keys = ['type', 'host', 'dbname', 'username', 'password'];
$values = ['mysql', 'localhost', 'phpedu', 'root', 'root'];
$res = array_combine($keys, $values);
printf('<pre>%s</pre>', print_r($res,true));
echo '<hr>';
$default= ['host'=>'localhost', 'username'=>'root', 'password'=>'root'];
$custom = ['username'=>'admin', 'password'=>'123456'];
$res = array_merge($default, $custom);
printf('<pre>%s</pre>', print_r($res,true));
echo '<hr>';
$arr = range(1,20);
$res = array_chunk($arr, 6);
printf('<pre>%s</pre>', print_r($res,true));
2.9 自动生成
| 序号 |
函数 |
描述 |
| 1 |
array_fill |
用给定的值填充数组 |
| 2 |
array_fill_keys |
使用指定的键和值填充数组 |
| 3 |
array_pad |
以指定长度将一个值填充进数组 |
示例:
$res = array_fill(2, 5, 'demo');
printf('<pre>%s</pre>', print_r($res, true));
echo '<hr>';
$keys = ['id', 'name', 'age', 'email'];
$res = array_fill_keys($keys, 'demo');
printf('<pre>%s</pre>', print_r($res, true));
echo '<hr>';
$arr = ['apple', 'dell', 'thinkpad'];
$res = array_pad($arr, 6, 'computer');
printf('<pre>%s</pre>', print_r($res, true));
2.10 类型转换
| 序号 |
函数 |
描述 |
| 1 |
list |
将数组中的值赋予一组变量(类似解构赋值) |
| 2 |
implode |
将数组元素按指定字符拼装成字符串 |
| 3 |
explode |
将字符串分割为数组 |
| 4 |
extract |
将关联数组拆分成变量名值对 |
| 5 |
compact |
将一组变量名值对拼装成一个关联数组键值对 |
示例:
<?php
list($id, $name) = [10, 'admin'];
printf('$id = %s, $name = %s <br>', $id, $name);
list('id'=>$id, 'name'=>$name) = ['id'=>20, 'name'=>'peter'];
printf('$id = %s, $name = %s <br>', $id, $name);
list(,,$email) = [10, 'admin', 'admin@php.cn'];
echo $email . '<br>';
echo '<hr>';
$arr = ['huawei', 'xiaome', 'apple', 'oppo', 'vivo'];
echo implode(', ', $arr);
echo '<hr>';
$str = 'blue, green, yellow, red, coral';
$res = explode(', ', $str);
printf('<pre>%s</pre>', print_r($res, true));
echo '<hr>';
$config = ['type'=>'mysql','host'=>'localhost', 'dbname'=>'phpedu', 'charset'=>'utf8'];
extract($config);
$dsn = sprintf('%s:host=%s; dbname=%s; charset=%s',$type,$host, $dbname, $charset);
echo $dsn , '<br>';
$pdo = new PDO($dsn, 'root', 'root');
var_dump($pdo);
echo '<hr>';
$id = 99;
$name = 'Peter Zhu';
$job = 'Lecture';
$res = ['id'=>$id, 'name'=>$name, 'job'=>$job];
printf('<pre>%s</pre>', print_r($res, true));
$res = compact('id', 'name', 'job');
printf('<pre>%s</pre>', print_r($res, true));
2.11 回调处理
| 序号 |
函数 |
描述 |
| 1 |
array_filter |
用回调函数过滤数组中的单元 |
| 2 |
array_map |
为数组的每个元素应用回调函数 |
| 3 |
array_reduce |
用回调函数迭代地将数组简化为单一的值 |
| 4 |
array_walk |
使用用户自定义函数对数组中的每个元素做回调处理 |
示例:
<?php
$arr = [150,'php', true,[4,5,6], (new class {}), [], null, false,'', 0, '0'];
$res = array_filter($arr);
printf('<pre>%s</pre>', print_r($res,true));
$res = array_filter($arr, function($value) {
return is_scalar($value);
});
printf('<pre>%s</pre>', print_r($res,true));
echo '<hr>';
$arr = ['php',[3,4,5], (new class {public $name='电脑';public $price=8888;}), 15, 20];
$res = array_map(function ($item){
switch (gettype($item)) {
case 'object':
$item = get_object_vars($item);
case 'array':
$item = implode(', ', $item);
}
return $item;
}, $arr);
printf('<pre>%s</pre>', print_r($res,true));
$keys = ['host', 'username', 'password'];
$values = ['localhost', 'root', '123456'];
$res = array_combine($keys, $values);
printf('<pre>%s</pre>', print_r($res,true));
$res = array_map(function ($value1, $value2) {
return [$value1 => $value2];
}, $keys, $values);
printf('<pre>%s</pre>', print_r($res,true));
echo '用户名: ' . $res[1]['username'];
echo '<hr>';
function my_array_reduce($array, $callback, $init=null)
{
$prev = $init;
foreach($array as $current){
$prev = $callback($prev, $current);
}
return $prev;
}
$result = my_array_reduce($res, function ($prev, $current) {
$key = key($current);
$value = current($current);
$prev[$key] = $value;
return $prev;
});
printf('自定义数组迭代函数返回值:<pre>%s</pre>', print_r($result,true));
echo '<hr>';
$res = array_reduce($res, function ($prev, $current) {
$key = key($current);
$value = current($current);
$prev[$key] = $value;
return $prev;
});
printf('内置数组迭代函数返回值:<pre>%s</pre>', print_r($res,true));
echo '<hr>';
$res = ['id'=>123, 'name'=>'peter', 'email'=>'peter@php.cn'];
array_walk($res, function ($value, $key, $color){
printf('[ %s ] => <span style="color:%s">%s</span><br>', $key, $color, $value);
}, 'red');