The following small series for you to share a PHP to get an array of the specified list of instances, has a good reference value, I hope to be helpful. Let's take a look at it with a little knitting.
For a multidimensional array (in the case of a two-dimensional array), it is often necessary to obtain a column, such as a few user data, is a two-dimensional array, now need to obtain the names of these users, there are many ways to implement:
$arr = array (' id ' = ' = ' 101 ', ' name ' = = ' qu ', ' age ' =>28), array (' id ' = ' 102 ', ' name ' = ' " Array (' id ' = ' = ' 103 ', ' name ' = ' = ' zheng ', ' age ' =>22), array (' id ' = ' 104 ', ' name ' = = ' Zhu ', ' age ' =>23)) ;
Method One: Use PHP built-in function array_column () to implement
Array Array_column (array $input, mixed $column _key [, Mixed $index _key])
Execute statement:
$result = Array_column ($arr, ' name '); Print_r ($result);
The results are as follows:
Array ( [0] = Qu [1] = you [2] = Zheng [3] = = Zhu)
If an optional parameter index_key is specified, the value of this column in the input array will be the key that returns the corresponding value in the array.
$result = Array_column ($arr, ' name ', ' id '); Print_r ($result);
The results are as follows:
Array ( [101] = Qu [102] = [ 103] = Zheng [104] = Zhu)
Method two: Using PHP built-in function array_map () implementation
Array Array_map (callable $callback, array $arr 1 [, Array $ ...]
Array_map () returns an array that contains all the cells in the ARR1 after the callback action. The first parameter is a callback function, and the return value is an array in which each element of the array (ARR1) is processed by a callback function (callback).
Declare a handler function first:
function Get_val ($arr) { return $arr [' name '];}
Then act on the Array_map () function:
$result = Array_map (' Get_val ', $arr); Print_r ($result);
The results of the implementation are as follows:
Array ( [0] = Qu [1] = you [2] = Zheng [3] = = Zhu)
Here the first parameter of Array_map () is a callback function, and is a well-defined known function, here we can also use the anonymous function like JS:
$result = Array_map (function ($v) { return $v [' name '];}, $arr);
The result of the execution is the same.
Above this PHP get array specified in the list of instances is a small part of the whole content to share to everyone, I hope to give you a reference, but also hope that we support PHP Chinese network.
Articles you may be interested in:
Analysis of usage of PHP array-type access interface arrayaccess
Analysis of PHP iterator interface iterator usage
The usage analysis of PHP aggregation iterator interface Iteratoraggregate