例如:<?php// 表示从数据库返回的记录集的数组 $records = array( array( 39;id' => 2135, 'first_name' => 'John', 'last_name' => 'Doe', ), array( 'id' => 3245, 'first_name' => 'Sally', 'last_name' => 'Smith', ), array( 'id' => 5342, 'first_name' => 'Jane', 'last_name' => 'Jones', ), array( 'id' => 5623, 'first_name' => 'Peter', 'last_name' => 'Doe', ) ); $first_names = array_column($records, 'first_name'); print_r($first_names);?>以上例程会输出:Array( [0] => John [1] => Sally [2] => Jane [3] => Peter)2 array_walk()函数利用用户自定义函数对数组中的每个元素做回调处理,实现当前功能的方法: $username = array(); array_walk($user, function($value, $key) use (&$username){ $username[] = $value['username']; });<?php $fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"); function test_alter(&$item1, $key, $prefix) { $item1 = "$prefix: $item1"; } function test_print($item2, $key){ echo "$key. $item2<br />\n"; } echo "Before ...:\n"; array_walk($fruits, 'test_print'); array_walk($fruits, 'test_alter', 'fruit'); echo "... and after:\n"; array_walk($fruits, 'test_print');?>以上例输出:Before ...:d. lemona. orangeb. bananac. apple... and after:d. fruit: lemona. fruit: orangeb. fruit: bananac. fruit: apple3 array_map()函数和array_walk() 浸染类似,将回调函数浸染到给天命组的单元上。$username = array();array_map(function($value) use (&$username){ $username[] = $value['username'];}, $user);<?php function cube($n) { return ($n $n $n); } $a = [1, 2, 3, 4, 5]; $b = array_map('cube', $a); print_r($b);?>这使得 $b 成为:Array( [0] => 1 [1] => 8 [2] => 27 [3] => 64 [4] => 125)例如,对付以下这个数组:$user = array( '0' => array('id' => 100, 'username' => 'a1'), '1' => array('id' => 101, 'username' => 'a2'), '2' => array('id' => 102, 'username' => 'a3'), '3' => array('id' => 103, 'username' => 'a4'), '4' => array('id' => 104, 'username' => 'a5'),);我们要提取个中的 usename 列,变成:$username = array('a1', 'a2', 'a3', 'a4', 'a5');4 foreach循环法foreach循环相对上面的方法效率轻微低一些,但大略随意马虎理解。$username = array();foreach ($user as $value) { $username[] = $value['username'];}5 array_map变种方法如下,意为把$user数组的每一项值的开头值移出,并获取移除的值作为新数组。把稳此时新数组$username的键仍是原数组$user的键,如下。$username = array_map('array_shift', $user);把稳:该功能会获取$user中的 id 列,而不是 username 列。其余,如果须要获取二维数组每一项的开头列或结尾列,也可以这样做:$username = array_map('reset', $user);$username = array_map('end', $user);这三个变种方法浸染比较局限,仅在获取第一列或末了一列的时候有用,在繁芜的数组中就难以发挥浸染了。6 array_walk_recursive函数法用array_walk_recursive()函数就非常灵巧,可以把任意维度的数组转换成一维数组。$result = [];array_walk_recursive($user, function($value) use (&$result) { array_push($result, $value);});例如,下面这个多维数组:$user4 = array( 'a' => array(100, 'a1'), 'b' => array(101, 'a2'), 'c' => array( 'd' => array(102, 'a3'), 'e' => array(103, 'a4'), ),);用这个方法后就变成:$result = array(100, 'a1', 101, 'a2', 102, 'a3', 103, 'a4');1 array_column()用PHP内置的 array_column() 函数是最大略的方法,限定是PHP版本必须是5.5.0及以上版本。