Analysis of the relationship between Array_map and Array_column in PHP

Source: Internet
Author: User

Analysis of the relationship between Array_map and Array_column in PHP

Submission: shichen2014 font: [Increase decrease] Type: Reprint time: 2014-08-19 I want to comment

This article mainly introduces the relationship between Array_map and Array_column in PHP, for PHP Beginners is a more practical concept, the need for friends can refer to the following

This paper analyzes the relationship between Array_map and Array_column in PHP by example, and analyzes the following:

Array_map () and Array_column () use the following:

Array_map (); function The callback function on the cell of the given array
Array_column (); Fast implementation: converting two-dimensional arrays to one-dimensional arrays

The format of the Array_column () function is:

Array Array_column (array $input, mixed $column _key [, Mixed $index _key]);

Returns a column with a value of Column_key in the input array; If an optional parameter index_key is specified, the corresponding key in the returned array is the value of the input array value Index_key.

Example code one:

?
12345678910111213141516171819202122232425 $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);

Output:

?
1234567 Array(  [0] => John  [1] => Sally  [2] => Jane  [3] => Peter)

Example code two:

?
12 $last_names= array_column($records, ‘last_name‘, ‘id‘);print_r($last_names);

Output:

?
1234567 Array(  [2135] => Doe  [3245] => Smith  [5342] => Jones  [5623] => Doe)

When no Array_column () function is in the case,

Use Array_map () to implement example one:

?
123 $a = array_map ( function ( $element Code class= "PHP Comments" data-garbage= "true" >//$records passed in as a parameter to the callback function return $element [  Code class= "PHP string" > ' last_name ' ); //returns the last_name corresponding value of the array element },             $records ); //array_map returns an array, equivalent to depositing each $element[' last_name ' into a new array, Therefore, the new index

Use the Foreach implementation example one:

?
1234 foreach($recordsas$v){  $b[] = $v[‘last_name‘];}
?
123456 使用foreach实现例二:$c = array();foreach($records as $k=>$v){ $c += array($v[‘id‘]=>$v[‘last_name‘]); //使用+运算符, 以追加的形式(不改变原数组索引), 合并组装的数组}                 //若使用array_merge,数字键名将被重新编号

A typical two-dimensional array in the fetched data, Array_column () can be done with a single value corresponding to the value in the data, but in the case of a more complex array structure, foreach will make you more flexible, but it is always preferred to use system functions first.

Analysis of the relationship between Array_map and Array_column in PHP

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.