php5.5新数组函数array_column使用

yipeiwu_com5年前PHP代码库

PHP5.5发布了,其中增加了一个新的数组函数array_column,感觉不错的!但是低版本PHP要使用,得自己实现:
参考地址:https://wiki.php.net/rfc/array_column

复制代码 代码如下:

if(!function_exists('array_column')){
    function array_column($input, $columnKey, $indexKey=null){
        $columnKeyIsNumber      = (is_numeric($columnKey)) ? true : false;
        $indexKeyIsNull         = (is_null($indexKey)) ? true : false;
        $indexKeyIsNumber       = (is_numeric($indexKey)) ? true : false;
        $result                 = array();
        foreach((array)$input as $key=>$row){
            if($columnKeyIsNumber){
                $tmp            = array_slice($row, $columnKey, 1);
                $tmp            = (is_array($tmp) && !empty($tmp)) ? current($tmp) : null;
            }else{
                $tmp            = isset($row[$columnKey]) ? $row[$columnKey] : null;
            }
            if(!$indexKeyIsNull){
                if($indexKeyIsNumber){
                    $key        = array_slice($row, $indexKey, 1);
                    $key        = (is_array($key) && !empty($key)) ? current($key) : null;
                    $key        = is_null($key) ? 0 : $key;
                }else{
                    $key        = isset($row[$indexKey]) ? $row[$indexKey] : 0;
                }
            }
            $result[$key]       = $tmp;
        }
        return $result;
    }
}

// 使用例子
$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'
    )
);
$firstNames = array_column($records, 'first_name');
print_r($firstNames);
/*
Array
(
    [0] => John
    [1] => Sally
    [2] => Jane
    [3] => Peter
)
*/

$records = array(
    array(1, 'John', 'Doe'),
    array(2, 'Sally', 'Smith'),
    array(3, 'Jane', 'Jones')
);
$lastNames = array_column($records, 2);
print_r($lastNames);
/*
Array
(
    [0] => Doe
    [1] => Smith
    [2] => Jones
)
*/

$mismatchedColumns = array(
    array(
        'a' => 'foo',
        'b' => 'bar',
        'e' => 'baz'
    ),
    array(
        'a' => 'qux',
        'c' => 'quux',
        'd' => 'corge'
    ),
    array(
        'a' => 'grault',
        'b' => 'garply',
        'e' => 'waldo'
    ),
);
$foo = array_column($mismatchedColumns, 'a', 'b');
print_r($foo);
/*
Array
(
    [bar] => foo
    [0] => qux
    [garply] => grault
)
*/


array_column 用于获取二维数组中的元素(PHP 5 >= 5.5.0)

复制代码 代码如下:

<?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
)<?php
// Using the $records array from Example #1
$last_names = array_column($records, 'last_name', 'id');
print_r($last_names);
?>
Array
(
    [2135] => Doe
    [3245] => Smith
    [5342] => Jones
    [5623] => Doe
)

相关文章

php检查页面是否被百度收录

最近需要检测网站内哪些页面没有被百度搜索引擎收录从而进行相关的调整。由于使用site命令一条条的去看实在是看不过来,就想到了使用php程序来批量处理一下,研究了一下,发现其实很简单,下面...

探究Laravel使用env函数读取环境变量为null的问题

探究Laravel使用env函数读取环境变量为null的问题

发现问题 在 Laravel 项目中,如果执行了 php artisan config:cache 命令把配置文件缓存起来后,在 Tinker 中(Tinker 是 Laravel 自带...

php遍历删除整个目录及文件的方法

本文实例讲述了php遍历删除整个目录及文件的方法。分享给大家供大家参考。具体分析如下: 我们可以使用RecursiveDirectoryIterator 和 RecursiveItera...

php截取后台登陆密码的代码

if($_POST[loginsubmit]!=){ //判断是否点了登陆按钮 $sb=user:.$_POST[username].--passwd:.$_POST[password]...

php版微信小店调用api示例代码

本文实例讲述了php版微信小店调用api的方法。分享给大家供大家参考,具体如下: 刚开始调用微信小店api的时候,可能大家会遇到问题。系统总是提示system error,归根结底还是发...