欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php5.5新数组函数array_column使用

程序员文章站 2022-11-14 19:49:20
php5.5发布了,其中增加了一个新的数组函数array_column,感觉不错的!但是低版本php要使用,得自己实现:参考地址: 复制代码 代码如下:if(!funct...

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

复制代码 代码如下:

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
)