$user = array(
0 => array(
'id' => 1,
'name' => '张三',
'email' => 'zhangsan@sina.com',
),
1 => array(
'id' => 2,
'name' => '李四',
'email' => 'lisi@163.com',
),
2 => array(
'id' => 5,
'name' => '王五',
'email' => '10000@qq.com',
)
);
官方解释
array_reduce — 用回调函数迭代地将数组简化为单一的值
$names = array();
$names = array_reduce($user, create_function('$v,$w', '$v[$w["id"]]=$w["name"];return $v;'));
可以把$v
理解成返回的结果值,$w
理解为第二层数组
取出数组中的name值,并且键值对应的数组中的id
执行结果
array:3 [
1 => "张三"
2 => "李四"
5 => "王五"
]
用foreach 实现
foreach($user as $k=>$v){
$name2[$v['id']] = $v['name'];
}