Php returns the specified column in the array (the default function of php5.5.0, array_column (), in php

Source: Internet
Author: User
Array_column () returns the column with the specified key name in the array (PHP55.5.0) array_column-returns a column specified in the array. What if php version is earlier than 5.5.0? We can customize the following code from onethinkOneThinkThinkPHP array_column () to return the column (PHP 5 = 5.5.0) with the specified key name in the array array_column-to return the specified column in the array

What if php version is earlier than 5.5.0? We can customize the following code from onethink OneThink ThinkPHP.

Array_column () returns the column with the specified key name in the array.

(PHP 5> = 5.5.0)

Array_column-returns a specified column in the array.

What if php version is earlier than 5.5.0? We customize

The following code is taken from onethink OneThink ThinkPHP.

/*** Return a specified column in the array * http://www.onethink.cn */Application/Common/function. php ** array_column-PHP 5> = 5.5.0 default function * PHP 5 <5.5.0, use the custom function ** @ access public * @ param array $ input to retrieve the multi-dimensional array of the array Column (or result set) * @ param string $ columnKey the column to be returned. It can be the index of the index array or the key of the column associated with the array. It can also be NULL. In this case, the entire array is returned (it works very well when the indexKey parameter is used to reset the array key) * @ param string $ indexKey is used as the column for returning the index/Key of the array, it can be an integer index of the column or a string key value. * @ Return array */if (! Function_exists ('array _ column ') {function array_column (array $ input, $ columnKey, $ indexKey = null) {$ result = array (); if (null ===$ indexKey) {if (null ===$ columnKey) {$ result = array_values ($ input);} else {foreach ($ input as $ row) {$ result [] = $ row [$ columnKey] ;}} else {if (null ===$ columnKey) {foreach ($ input as $ row) {$ result [$ row [$ indexKey] = $ row ;}} else {foreach ($ input as $ row) {$ result [$ row [$ indexKey] = $ row [$ columnKey] ;}} return $ result ;}}
 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.