熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> PHP編程 >> 正文

php5.5新數組函數array

2013-11-15 12:27:05  來源: PHP編程 

  PHP發布了其中增加了一個新的數組函數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 );
                $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 );
                    $key        = (is_array($key) && !empty($key)) ? current($key) : null;
                    $key        = is_null($key) ? : $key;
                }else{
                    $key        = isset($row[$indexKey]) ? $row[$indexKey] : ;
                }
            }
            $result[$key]       = $tmp;
        }
        return $result;
    }
}

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

$records = array(
    array( John Doe)
    array( Sally Smith)
    array( Jane Jones)
);
$lastNames = array_column($records );
print_r($lastNames);
/*
Array
(
    [] => Doe
    [] => Smith
    [] => 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
    [] => qux
    [garply] => grault
)
*/

  
array_column 用於獲取二維數組中的元素(PHP >= )

復制代碼 代碼如下:
<?php
// Array representing a possible record set returned from a database
$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);
?>
Array
(
    [] => John
    [] => Sally
    [] => Jane
    [] => Peter
)<?php
// Using the $records array from Example #
$last_names = array_column($records last_name id);
print_r($last_names);
?>
Array
(
    [] => Doe
    [] => Smith
    [] => Jones
    [] => Doe
)
From:http://tw.wingwit.com/Article/program/PHP/201311/20974.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.