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:
- $ids = array();
- $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:
- $names = array();
- $names = array_reduce($user, create_function('$v,$w', '$v[$w["id"]]=$w["name"];return $v;' ));
Get the result:
- array(
- 1 => 'Zhang San',
- 2 => '李思',
- 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