PHP obtains an instance of a specified column in the array and an instance of a column in the php array.

Source: Internet
Author: User

PHP obtains an instance of a specified column in the array and an instance of a column in the php array.

For a multi-dimensional array (taking a two-dimensional array as an example), you often need to obtain a column. For example, there are several pieces of user data, which is a two-dimensional array, now you need to get the names of these users. There are multiple methods to achieve this:

$arr = array(array('id'=>'101','name'=>'qu','age'=>28),array('id'=>'102','name'=>'you','age'=>24),array('id'=>'103','name'=>'zheng','age'=>22),array('id'=>'104','name'=>'zhu','age'=>23));

Method 1: Use the PHP built-in function array_column () to implement

array array_column ( array $input , mixed $column_key [, mixed $index_key ] ) 

Execute the statement:

$result = array_column($arr,'name'); print_r($result); 

The result is as follows:

Array (  [0] => qu  [1] => you  [2] => zheng  [3] => zhu ) 

If index_key is specified, the value of this column in the input array is used as the key to return the corresponding value in the array.

$result = array_column($arr,'name','id'); print_r($result); 

The result is as follows:

Array (  [101] => qu  [102] => you  [103] => zheng  [104] => zhu ) 

Method 2: Use the PHP built-in function array_map () to implement

array array_map ( callable $callback , array $arr1 [, array $... ] ) 

Array_map () returns an array that contains all the elements in arr1 that have passed the callback function. The first parameter is a callback function, and the returned value is an array. Each element of the array is processed by the callback function (callback) for each element in the array (arr1.

Declare a processing function first:

function get_val($arr){  return $arr['name']; } 

Then act on the array_map () function:

$result = array_map('get_val',$arr); print_r($result); 

The execution result is as follows:

Array (  [0] => qu  [1] => you  [2] => zheng  [3] => zhu ) 

Here, the first parameter of array_map () is a callback function, which is a well-known function defined in advance. Here we can also use an anonymous function like js:

$result = array_map(function($v){  return $v['name']; },$arr); 

The execution results are the same.

The above PHP obtains all the content shared by the editor in the array. I hope you can give us a reference and support the house of friends.

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.