array_column 返回数组中指定的一列
发表日期:2021-07-01 08:57:05 | 来源: | | 浏览(594) 分类:数组 函数
array_column
(PHP 5 >= 5.5.0, PHP 7, PHP 8)
array_column — 返回数组中指定的一列
说明
$input
, mixed $column_key
, mixed $index_key
= null): array
array_column()
返回input
数组中键值为column_key
的列,
如果指定了可选参数index_key
,那么input
数组中的这一列的值将作为返回数组中对应值的键。
参数
-
input
-
需要取出数组列的多维数组。 如果提供的是包含一组对象的数组,只有 public 属性会被直接取出。 为了也能取出 private 和 protected 属性,类必须实现 __get() 和 __isset() 魔术方法。
-
column_key
-
需要返回值的列,它可以是索引数组的列索引,或者是关联数组的列的键,也可以是属性名。 也可以是
null
,此时将返回整个数组(配合index_key
参数来重置数组键的时候,非常管用) -
index_key
-
作为返回数组的索引/键的列,它可以是该列的整数索引,或者字符串键值。
返回值
从多维数组中返回单列数组。
更新日志
版本 | 说明 |
---|---|
7.0.0 |
input 参数现在可以是包含对象的数组。
|
范例
示例 #1 从结果集中取出first names列
1 | <?php |
2 | // Array representing a possible record set returned from a database$records = array( array( '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', )); |
3 | $first_names = array_column( $records , 'first_name' ); |
4 | print_r( $first_names ); |
5 | ?> |
以上例程会输出:
Array ( [0] => John [1] => Sally [2] => Jane [3] => Peter )
示例 #2 从结果集中总取出last names列,用相应的id作为键值
1 | <?php |
2 | // Using the $records array from Example #1$last_names = array_column($records, 'last_name', 'id'); |
3 | print_r( $last_names ); |
4 | ?> |
以上例程会输出:
Array ( [2135] => Doe [3245] => Smith [5342] => Jones [5623] => Doe )
示例 #3 username 列是从对象获取 public 的 "username" 属性
01 | <?php |
02 | class User{ |
03 | public $username ; |
04 | public function __construct(string $username ) { |
05 | $this ->username = $username ; |
06 | } |
07 | } |
08 | $users = [ new User( 'user 1' ), new User( 'user 2' ), new User( 'user 3' ),]; |
09 | print_r(array_column( $users , 'username' )); |
10 | ?> |
以上例程会输出:
Array ( [0] => user 1 [1] => user 2 [2] => user 3 )
示例 #4 获取 username 列,从对象通过魔术方法 __get() 获取 private 的 "username" 属性。
01 | <?php |
02 | class Person{ |
03 | private $name ; |
04 | public function __construct(string $name ) { |
05 | $this ->name = $name ; |
06 | } |
07 | public function __get( $prop ) { |
08 | return $this -> $prop ; |
09 | } |
10 | public function __isset( $prop ) : bool { |
11 | return isset( $this -> $prop ); |
12 | } |
13 | } |
14 | $people = [ new Person( 'Fred' ), new Person( 'Jane' ), new Person( 'John' ),]; |
15 | print_r(array_column( $people , 'name' )); |
16 | ?> |
以上例程会输出:
Array ( [0] => Fred [1] => Jane [2] => John )
- PHP(0)
- PHP杂项(34)
- PHP基础-李炎恢系列课程(20)
- 中文函数手册(0)
- 错误处理 函数(13)
- OPcache 函数(6)
- PHP 选项/信息 函数(54)
- Zip 函数(10)
- Hash 函数(15)
- OpenSSL 函数(63)
- Date/Time 函数(51)
- 目录函数(9)
- Fileinfo 函数(6)
- iconv 函数(11)
- 文件系统函数(81)
- 多字节字符串 函数(57)
- GD 和图像处理 函数(114)
- 可交换图像信息(5)
- Math 函数(50)
- 程序执行函数(11)
- PCNTL 函数(23)
- JSON 函数(4)
- SPL 函数(15)
- URL 函数(10)
- cURL 函数(32)
- 网络 函数(33)
- FTP 函数(36)
- Session 函数(23)
- PCRE 函数(11)
- PCRE 正则语法(19)
- 数组 函数(81)
- array_change_key_case 将数组中的所有键名修改为全大写或小写(0)
- array_chunk 将一个数组分割成多个(0)
- array_column 返回数组中指定的一列(0)
- array_combine 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值(0)
- array_count_values 统计数组中所有的值(0)
- array_diff_assoc 带索引检查计算数组的差集(0)
- array_diff_key 使用键名比较计算数组的差集(0)
- array_diff_uassoc 用用户提供的回调函数做索引检查来计算数组的差集(0)
- array_diff_ukey 用回调函数对键名比较计算数组的差集(0)
- array_diff 计算数组的差集(0)
- array_fill_keys 使用指定的键和值填充数组(0)
- array_fill 用给定的值填充数组(0)
- array_filter 使用回调函数过滤数组的元素(0)
- array_flip 交换数组中的键和值(0)
- array_intersect_assoc 带索引检查计算数组的交集(0)
- array_intersect_key 使用键名比较计算数组的交集(0)
- array_intersect_uassoc 带索引检查计算数组的交集,用回调函数比较索引(0)
- array_intersect_ukey 在键名上使用回调函数来比较计算数组的交集(0)
- array_intersect 计算数组的交集(0)
- array_key_exists 检查数组里是否有指定的键名或索引(0)
- array_key_first 获取指定数组的第一个键值(0)
- array_key_last 获取一个数组的最后一个键值(0)
- array_keys 返回数组中部分的或所有的键名(0)
- array_map 为数组的每个元素应用回调函数(0)
- array_merge_recursive 递归地合并一个或多个数组(0)
- array_merge 合并一个或多个数组(0)
- array_multisort 对多个数组或多维数组进行排序(0)
- array_pad 以指定长度将一个值填充进数组(0)
- array_pop 弹出数组最后一个单元(出栈)(0)
- array_product 计算数组中所有值的乘积(0)
- array_push 将一个或多个单元压入数组的末尾(入栈)(0)
- array_rand 从数组中随机取出一个或多个随机键(0)
- array_reduce 用回调函数迭代地将数组简化为单一的值(0)
- array_replace_recursive 使用传递的数组递归替换第一个数组的元素(0)
- array_replace 使用传递的数组替换第一个数组的元素(0)
- array_reverse 返回单元顺序相反的数组(0)
- array_search 在数组中搜索给定的值,如果成功则返回首个相应的键名(0)
- array_shift 将数组开头的单元移出数组(0)
- array_slice 从数组中取出一段(0)
- array_splice 去掉数组中的某一部分并用其它值取代(0)
- array_sum 对数组中所有值求和(0)
- array_udiff_assoc 带索引检查计算数组的差集,用回调函数比较数据(0)
- array_udiff_uassoc 带索引检查计算数组的差集,用回调函数比较数据和索引(0)
- array_udiff 用回调函数比较数据来计算数组的差集(0)
- array_uintersect_assoc 带索引检查计算数组的交集,用回调函数比较数据(0)
- array_uintersect_uassoc 带索引检查计算数组的交集,用单独的回调函数比较数据和索引(0)
- array_uintersect 计算数组的交集,用回调函数比较数据(0)
- array_unique 移除数组中重复的值(0)
- array_unshift 在数组开头插入一个或多个单元(0)
- array_values 返回数组中所有的值(0)
- array_walk_recursive 对数组中的每个成员递归地应用用户函数(0)
- array_walk 使用用户自定义函数对数组中的每个元素做回调处理(0)
- array 新建一个数组(0)
- arsort 对数组进行降向排序并保持索引关系(0)
- asort 对数组进行升序排序并保持索引关系(0)
- compact 建立一个数组,包括变量名和它们的值(0)
- count 计算数组中的单元数目,或对象中的属性个数(0)
- current 返回数组中的当前值(0)
- each 返回数组中当前的键/值对并将数组指针向前移动一步(0)
- end 将数组的内部指针指向最后一个单元(0)
- extract 从数组中将变量导入到当前的符号表(0)
- in_array 检查数组中是否存在某个值(0)
- key_exists 别名 array_key_exists()(0)
- key 从关联数组中取得键名(0)
- krsort 对数组按照键名逆向排序(0)
- ksort 对数组根据键名升序排序(0)
- list 把数组中的值赋给一组变量(0)
- natcasesort 用“自然排序”算法对数组进行不区分大小写字母的排序(0)
- natsort 用“自然排序”算法对数组排序(0)
- next 将数组中的内部指针向前移动一位(0)
- pos current() 的别名(0)
- prev 将数组的内部指针倒回一位(0)
- range 根据范围创建数组,包含指定的元素(0)
- reset 将数组的内部指针指向第一个单元(0)
- rsort 对数组降序排序(0)
- shuffle 打乱数组(0)
- sizeof count() 的别名(0)
- sort 对数组升序排序(0)
- uasort 使用用户自定义的比较函数对数组中的值进行排序并保持索引关联(0)
- uksort 使用用户自定义的比较函数对数组中的键名进行排序(0)
- usort 使用用户自定义的比较函数对数组中的值进行排序(0)
- 类/对象 函数(18)
- 函数处理 函数(13)
- 变量处理 函数(37)
- SimpleXML 函数(3)
- 杂项 函数(31)
- 字符串 函数(101)