array_column() 返回数组中指定键名的列 (PHP 5 =5.5.0) array_column—返回数组中指定的一列
如果php版本小于5.5.0怎么办呢?我们自定义一个 以下代码摘自onethink OneThink ThinkPHP
array_column() 返回数组中指定键名的列
(PHP 5 >= 5.5.0)
array_column — 返回数组中指定的一列
如果php版本小于5.5.0怎么办呢?我们自定义一个
以下代码摘自onethink OneThink ThinkPHP
/** * 返回数组中指定的一列 * http://www.onethink.cn * /Application/Common/Common/function.php * * array_column — PHP 5 >= 5.5.0 默认函数 * PHP 5 < 5.5.0 则使用自定义函数 * * @access public * @param array $input 需要取出数组列的多维数组(或结果集) * @param string $columnKey 需要返回值的列,它可以是索引数组的列索引,或者是关联数组的列的键。也可以是NULL,此时将返回整个数组(配合indexKey参数来重置数组键的时候,非常管用) * @param string $indexKey 作为返回数组的索引/键的列,它可以是该列的整数索引,或者字符串键值。 * @return array */ if (! function_exists('array_column')) { function array_column(array $input, $columnKey, $indexKey = null) { $result = array(); if (null === $indexKey) { if (null === $columnKey) { $result = array_values($input); } else { foreach ($input as $row) { $result[] = $row[$columnKey]; } } } else { if (null === $columnKey) { foreach ($input as $row) { $result[$row[$indexKey]] = $row; } } else { foreach ($input as $row) { $result[$row[$indexKey]] = $row[$columnKey]; } } } return $result; } } <?php // 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', ) ); $first_names = array_column($records, 'first_name'); print_r($first_names); ?> Array ( [0] => John [1] => Sally [2] => Jane [3] => Peter )
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章
Windows 11 KB5054979中的新功能以及如何解决更新问题
3 周前ByDDD
如何修复KB5055523无法在Windows 11中安装?
2 周前ByDDD
Inzoi:如何申请学校和大学
4 周前ByDDD
如何修复KB5055518无法在Windows 10中安装?
2 周前ByDDD
在哪里可以找到Atomfall中的站点办公室钥匙
4 周前ByDDD

热工具

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

Atom编辑器mac版下载
最流行的的开源编辑器

Dreamweaver CS6
视觉化网页开发工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。