PHP's method of extracting a multidimensional array of specified columns

Source: Internet
Author: User

Foreword: Sometimes in the development of this problem, we need to have a regular multidimensional array according to the vertical (column), the following methods are available:

We will take the array of polygons to deal with:

1 $arr Array (2     Array (' id ' = 1, ' name ' = ' name1 '),3     array(' id ' = 2, ' name ' = ' name2 '),4     array(' id ' = = 3, ' name ' = ' Name3 '),5     Array (' id ' = 4, ' name ' = ' name4 '),6     array(' id ' = 5, ' name ' = ' name5 '),7 );

Required results: $name _list = Array (' name1 ', ' name2 ', ' name3 ', ' name4 ', ' name5 ');

1. Using Array_column ()

PHP added a dedicated function Array_column () after version 5.5.0

Method:

1 $name _list = Array_column ($arr, ' name ');

2. Array_walk () method

Array_walk () uses a user-defined function to do callback processing for each element in the array

1 $name _list Array (); 2 Array_walk ($arrfunction($value$key use (&$name _list  ) {  3     $name _list$value[' name ']; 4 });

3. Array_map () method

The Array_map () function () is similar to Array_walk, and the callback function is acting on the cell of the given array

1 $name _list Array (); 2 Array_map (function($value use (&$name _list) {3     $ Name_list$value[' name ']; 4 $arr);

4. Foreach Loop Traversal method

The foreach () loop is slightly less efficient than the above method

1 $name _list Array (); 2 foreach ($arras$value) {3     $name _list $value [' name ']; 4 }

5. Array_map Variant

Move the beginning value of each item value of the $arr array out and get the removed value as the new array. Note that the key of the new array $name_list is still the key of the original array $arr

1 $name _list=Array_map(' Array_shift ',$arr);2 //Note: This feature gets the ID column in $arr, not the Name column. 3 In addition, you can do this if you need to get the beginning or end columns of each item in a two-dimensional array:4 5 $name _list=Array_map(' Reset ',$arr);6 $name _list=Array_map(' End ',$arr);7 8 //These three variant methods are limited in function and only useful when getting the first or last column,9 //complex arrays are difficult to work with. 

PHP's method of extracting a multidimensional array of specified columns

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.