php5.5新數組函數array

來源:互聯網
上載者:User

PHP5.5發布了,其中增加了一個新的數組函數array_column,感覺不錯的!但是低版本PHP要使用,得自己實現:
參考地址:https://wiki.php.net/rfc/array_column

 

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 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)*/


相關文章

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。