Home >php教程 >php手册 >PHP gets a collection of a certain key in a two-dimensional array

PHP gets a collection of a certain key in a two-dimensional array

WBOY
WBOYOriginal
2016-08-26 10:13:051864browse

This article is for code sharing. I saw some "big cow" code at work and wanted to share it.

This is specifically, the following two-dimensional array is read from the library.

Code List:

[php] view plain copy
PHP gets a collection of a certain key in a two-dimensional arrayPHP gets a collection of a certain key in a two-dimensional array
  1. $user = array(
  2. 0 => array(
  3. ''Id' = & gt; 1,
  4. E'Name '= & gt; ' Zhang San ',
  5. I'email '= & gt; 'zhangsan@sina.com',
  6. ), 1 => array(
  7. ''Id' = & gt; 2,
  8.                                                                                                                                                                                                                         'name'       => " ),
  9. 2 =>
  10. array( ''Id' = & gt; 5,
  11. E'Name '= & gt; ' king five ',
  12.                                                                                                                                                                                                                        with 'email' =>
  13. ),  …  );
  14. The above array format is mainly familiar to everyone who has played with PHP+MYSQL.
  15. So, now there are two requirements:
  16. 1) Get the collection of index "id" and save it as a one-bit array, that is, get array(1,2,5) I don’t know how my friends would write it?
  17. If it were the way I wrote it in the past, I would directly use foreach, and then use array_push to stuff into array variables one by one. This can also be achieved. But this way of writing affects performance, because using PHP's native functions is definitely more efficient than looping. Code List:
  18. [php]
  19. view plain
copy



$ids =

array();

$ids = array_map('array_shift', $user);


The above code can get the results we want. Please refer to the manual for the use of functions.

In fact, there is another solution here, using the array_column function, but this function requires PHP version requirements, (PHP 5 >= 5.5.0)

Code List:

[php] view plain copy
PHP gets a collection of a certain key in a two-dimensional arrayPHP gets a collection of a certain key in a two-dimensional array
  1. $ids = array();
  2. $ids = array_column($user, 'id');

In this case, the efficiency will definitely be higher.


2)Get the set of index "name" and save it as a one-bit array, that is, get array('Zhang San','Li Si','Wang Wu')

According to my previous writing method, it is still the same foreach, and then array_push is inserted into an array variable one by one. See the efficient code listing.

Code List:

[php] view plain copy
PHP gets a collection of a certain key in a two-dimensional arrayPHP gets a collection of a certain key in a two-dimensional array
  1. $names = array();
  2. $names = array_reduce($user, create_function('$v,$w', '$v[$w["id"]]=$w["name"];return $v;' ));


Get the result:

[php] view plain copy
PHP gets a collection of a certain key in a two-dimensional arrayPHP gets a collection of a certain key in a two-dimensional array
  1. array(
  2. 1 => 'Zhang San',
  3. 2 => '李思',
  4. 5 => '王五',
  5. );

Children's shoes that are often foreached, please correct them as soon as possible!

This article comes from CSDN, please indicate the source when reprinting! http://blog.csdn.net/liruxing1715/article/details/22925575

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn