Analysis of the Relationship between array_map and array_column in PHP

Source: Internet
Author: User
This article mainly introduces the Relationship Analysis Between array_map and array_column in PHP. It is a practical concept for beginners of PHP. For more information, see

This article mainly introduces the Relationship Analysis Between array_map and array_column in PHP. It is a practical concept for beginners of PHP. For more information, see

This article analyzes the relationship between array_map and array_column in PHP as an example. The specific analysis is as follows:

The usage of array_map () and array_column () is as follows:

Array_map (); calls the callback function to the unit of the given array.
Array_column (); quick implementation: convert a two-dimensional array into a one-dimensional array

The format of the array_column () function is:

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

Returns the columns whose values are column_key. If the optional parameter index_key is specified, the corresponding key in the returned array corresponds to the value of index_key in the input array.

Sample Code 1:

$ Records = 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:

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

Sample Code 2:

$ Last_names = array_column ($ records, 'last _ name', 'id'); print_r ($ last_names );

Output:

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

If the array_column () function is not available,

Use array_map () to implement Example 1:

$ A = array_map (function ($ element) {// $ records as the parameter to pass in the callback function return $ element ['last _ name']; // return the last_name value of the array element value}, $ records); // array_map returns an array, which is equivalent to storing each $ element ['last _ name'] into a new array, therefore, it is a new index.

Use foreach For Example 1:

Foreach ($ records as $ v) {$ B [] = $ v ['last _ name'];}

Use foreach For Example 2: $ c = array (); foreach ($ records as $ k => $ v) {$ c + = array ($ v ['id'] => $ v ['last _ name']); // use the + operator, in the form of appending (without changing the original array index), merged and assembled arrays} // If array_merge is used, the number key name will be re-numbered

In a typical two-dimensional array, array_column () can be used to match a single value with a value in the retrieved data, however, in the face of complicated array structures, foreach can make you more flexible, but the first choice is to use system functions first.

,

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.