PHP: how to obtain a set of keys in a two-dimensional array (high-performance search) _ PHP Tutorial

Source: Internet
Author: User
How does PHP obtain a set of keys in a two-dimensional array (high-performance search ). Share The PHP method to obtain a set of keys in a two-dimensional array. Specifically, for example, the next two-dimensional array is read from the database. Code: $ userarray (0 array: Share the PHP method for getting a set of keys in a two-dimensional array.

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

Code:

$ User = array (0 => array ('id' => 1, 'name' => 'Zhang San', 'Email '=> 'zhangsan @ sina.com ',), 1 => array ('id' => 2, 'name' => 'Lily', 'Email '=> 'lisi @ 163.com ',), 2 => array ('id' => 5, 'name' => 'Wang Wu ', 'email '=> '2017 @ qq.com ',),......);

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)
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:

$ids = array(); $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:

$ids = array(); $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.

Code:

$names = array(); $names = array_reduce($user, create_function('$v,$w', '$v[$w["id"]]=$w["name"];return $v;'));

Expected result:

Array (
1 => 'Zhang San ',
2 => 'Li Si ',
5 => 'Wang Wu ',
);

Evaluate to obtain a set of keys in a two-dimensional array. Specifically, for example, the next two-dimensional array is read from the database. Code: $ user = array (0 = array...

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.