This article for code sharing, but also in the work to see some "Daniel" code, do share.
specifically, such as the next two-dimensional array, is read from the library.
Code Listings:
. The code is as follows:
$user = Array (
0 => Array (
' id ' => 1,
' name ' => ' John ',
' email ' => ' zhangsan@sina.com ',
),
1 => Array (
' id ' => 2,
' name ' => ' Dick ',
' email ' => ' lisi@163.com ',
),
2 => Array (
' id ' => 5,
' name ' => ' Harry ',
' email ' => ' 10000@qq.com ',
),
......
);
the above array format, the main people have played php+mysql, must be very familiar with.
So, now there are two of these requirements:
1 Gets the collection of the index "ID" and saves it as an array, that is, the array (1,2,5)
I do not know how the friends will write it?
if it was previously my writing is directly foreach, and then Array_push one after another into an array variable plug. This can also be achieved. But this style of writing affects performance because using PHP's native functions is certainly more efficient than looping.
Code Listings:
. The code is as follows:
$ids = Array ();
$ids = Array_map (' Array_shift ', $user);
The above code can get the results we want, about the use of functions to see the manual.
In fact, there is also a scheme, using the Array_column function, but this function requires PHP version requirements, (PHP 5 >= 5.5.0)
Code Listings:
. The code is as follows:
$ids = Array ();
$ids = Array_column ($user, ' id ');
in that case, the efficiency will certainly be higher.
2 Gets the collection of index "name" and saves it as an array that gets array (' John ', ' Dick ', ' Harry ')
according to my previous writing, it's still the same foreach, and then Array_push each one into an array variable. See the efficient code list.
Code Listings:
. The code is as follows:
$names = Array ();
$names = Array_reduce ($user, Create_function (' $v, $w ', ' $v [$w [' id ']]= $w [' name '];return $v; ']);
Get the result:
. The code is as follows:
Array (
1 => ' John ',
2 => ' Dick ',
5 => ' Harry ',
);
often foreach children's shoes, quickly corrected it!