PHP obtains a set of keys in a two-dimensional array.

Source: Internet
Author: User

PHP obtains a set of keys in a two-dimensional array.

This article is for code sharing. I also see some "Daniel" code at work and share it with me.

Specifically, for example, the next two-dimensional array is read from the database.

Code List:

 

[Php]View plain copy
  1. $ User = array (
  2. 0 => array (
  3. 'Id' => 1,
  4. 'Name' => 'zhang san ',
  5. 'Email '=> 'zhangsan @ sina.com ',
  6. ),
  7. 1 => array (
  8. 'Id' => 2,
  9. 'Name' => 'Li si ',
  10. 'Email '=> 'lisi @ 163.com ',
  11. ),
  12. 2 => array (
  13. 'Id' => 5,
  14. 'Name' => 'wang 5 ',
  15. 'Email '=> '2017 @ qq.com ',
  16. ),
  17. ......
  18. );


The above array format is familiar with PHP + MYSQL.

 

Now there are two requirements:

1) obtain the index "id" set and save it as an array to obtain array (, 5)

I don't know what your friends will do?

If I used to write foreach directly, then array_push one by one into an array variable. This can also be achieved. However, such writing affects performance, because using native PHP functions is certainly more efficient than looping.

Code List:

 

[Php]View plain copy
  1. $ Ids = array ();
  2. $ Ids = array_map ('array _ shift ', $ user );


The above code can get the desired result. For more information about function usage, see the manual.

 

In fact, there is also a solution to use the array_column function, but this function requires PHP version requirements (PHP 5> = 5.5.0)

Code List:

 

[Php]View plain copy
  1. $ Ids = array ();
  2. $ Ids = array_column ($ user, 'id ');

 

 

In this case, the efficiency will certainly be higher.


2)Obtain the set of index "name" and save it as an array to obtain the array ('zhang san', 'Li si', 'wang wu ')

According to my previous writing method, I still use the same foreach method. Then, array_push is pushed one by one to an array variable. Please refer to the efficient code list.

Code List:

 

[Php]View plain copy
  1. $ Names = array ();
  2. $ Names = array_reduce ($ user, create_function ('$ v, $ W',' $ v [$ w ["id"] = $ w ["name"]; return $ v ;'));


Expected result:

 

 

[Php]View plain copy
  1. Array (
  2. 1 => 'zhang san ',
  3. 2 => 'Li si ',
  4. 5 => 'wang wu ',
  5. );

 

Often foreach's children's shoes. Please correct them!

This article is from CSDN. For more information, see the source! Http://blog.csdn.net/liruxing1715/article/details/22925575

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.