php5.5新数组函数array_column应用实例分享

最新的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; 
    } 
} 
//edit by www.jbxue.com

// 使用例子 
$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
// Edit by www.jbxue.com
$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
)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值