位置: 编程技术 - 正文

PHP数组函数array_column()用法 返回数组中指定的一列(php数组函数 菜鸟)

编辑:rootadmin

推荐整理分享PHP数组函数array_column()用法 返回数组中指定的一列(php数组函数 菜鸟),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:php数组函数,选班长,php的数组函数,php数组函数有哪些,php数组函数有哪些,php数组函数面试题,php数组函数大全,php数组函数有哪些,php数组函数面试题,内容如对您有帮助,希望把文章链接给更多的朋友!

array_column

(PHP 5 >= 5.5.0)

array_column — 返回数组中指定的一列

说明

arrayarray_column( array$input, mixed$column_key[, mixed$index_key] )

array_column()返回input数组中键值为column_key的列, 如果指定了可选参数index_key,那么input数组中的这一列的值将作为返回数组中对应值的键。

参数

input

需要取出数组列的多维数组(或结果集)

column_key

PHP数组函数array_column()用法 返回数组中指定的一列(php数组函数 菜鸟)

需要返回值的列,它可以是索引数组的列索引,或者是关联数组的列的键。 也可以是NULL,此时将返回整个数组(配合index_key参数来重置数组键的时候,非常管用)

index_key

作为返回数组的索引/键的列,它可以是该列的整数索引,或者字符串键值。

返回值

从多维数组中返回单列数组

范例

Example #1 从结果集中取出first names列

<?php//Arrayrepresentingapossiblerecordsetreturnedfromadatabase$records=array(array(&#;id&#;=>,&#;first_name&#;=>&#;John&#;,&#;last_name&#;=>&#;Doe&#;,),array(&#;id&#;=>,&#;first_name&#;=>&#;Sally&#;,&#;last_name&#;=>&#;Smith&#;,),array(&#;id&#;=>,&#;first_name&#;=>&#;Jane&#;,&#;last_name&#;=>&#;Jones&#;,),array(&#;id&#;=>,&#;first_name&#;=>&#;Peter&#;,&#;last_name&#;=>&#;Doe&#;,));$first_names=array_column($records,&#;first_name&#;);print_r($first_names);?>

以上例程会输出:

Example #2 从结果集中总取出last names列,用相应的id作为键值

<?php //Usingthe$recordsarrayfromExample#1 $last_names=array_column($records,&#;last_name&#;,&#;id&#;); print_r($last_names);?>

以上例程会输出:

本文链接地址:https://www.jiuchutong.com/biancheng/281096.html 转载请保留说明!

上一篇:PHP数组函数array_combine()的用法 用数组创建数组(php数组函数输出《咏雪》里有多少"片"字)

下一篇:PHP数组函数array_chunk()用法 将数组分割成多个(php数组函数题目)

免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

鄂ICP备2023003026号

网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

友情链接: 武汉网站建设 电脑维修 湖南楚通运网络