PHP is still quite common, so I studied the PHP two-dimensional array. next I will introduce you to the PHP to get the value set of a column in the two-dimensional array, if you are interested in the values of two-dimensional arrays of php arrays, you can study PHP arrays. When processing php arrays, the following two-dimensional arrays are frequently required:
$ Arr = array (1 => array ('id' => 5, 'name' => 'Zhang San'), 2 => array ('id' => 6, 'name' => 'Lily '));
The purpose is to obtain the set with key as name and get the result:
$ Arr2 = array (0 => 'Zhang San', 1 => 'Li si ');
There are several methods:
1: The simplest, foreach traversing the array:
foreach ($arr as $key => $value) {$arr2[] = $value['name'];}
2: The php method of array_map is used for a slightly less code:
$arr2 = array_map('array_shift',$arr);
Remove the value starting with each item of the $ arr array and return the value to be removed from each item, note that the key of the new array $ arr2 is still the key of the original array $ arr.
2.1: on the basis of Method 2, you can slightly open the brain hole. if you need to obtain the starting or ending columns of each item in a two-dimensional array, you can also do this:
$arr2 = array_map('reset',$arr);$arr2 = array_map('end',$arr);
Haha, it's very convenient.
3: You can also use the array_reduc e method, but the code is slightly larger. However, the imagination of this method (for other array value operations) is quite large:
$arr2 = array_reduce($arr, create_function('$result, $v', '$result[] = $v["name"];return $result;'));
The array_reduce method uses the callback function to iterate over the value of the array. create_function is used for an anonymous method to call back. The parameter $ result of this anonymous method is the value generated in the previous iteration, $ v is the current value. The internal implementation is to get the "name" value of each $ arr item and push it to the new $ result array;
4: In the end, this ultimate method is really cool. it can be done with one method, and it is very flexible:
$arr2 = array_column($arr, 'name');
The second parameter is the key name of the column to be obtained. is it very convenient? However, this method has a limit that the php version must be> = 5.5.0, to use this method in outdated projects, consider it.
PS: several methods for traversing two-dimensional arrays in php
<? Php // use for loop traversal $ arr2 = array ("zhang san", "20", "male"), array ("Li Si", "25 ", "Male"), array ("Wang Wu", "19", "female"), array ("Zhao Liu", "25", "female"); echo"
Name |
AgeGender |
"; For ($ I = 0; $ I <4; $ I ++) {echo"
"; For ($ j = 0; $ j <3; $ j ++) {echo"
"; Echo $ arr2 [$ I] [$ j]; echo" | ";} Echo"
"; Echo"";} Echo"
";?> // Use foreach to traverse <? Php $ arr = array ('one' => array ('name' => 'Zhang San', 'age' => '23 ', 'Sex' => 'male'), 'two' => array ('name' => 'Lily', 'age' => '43 ', 'Sex' => 'female '), 'Three' => array ('name' => '王', 'age' => '32 ', 'Sex' => 'male'), 'Four '=> array ('name' => 'Zhao Liu', 'age' => '12 ', 'Sex' => 'female '); foreach ($ arr as $ k => $ val) {echo $ val ['name']. $ val ['age']. $ val ['sex']."
";} Echo"
";?> <? Php $ arr = array ('one' => array ('name' => 'Zhang San', 'age' => '23 ', 'Sex' => 'male'), 'two' => array ('name' => 'Lily', 'age' => '43 ', 'Sex' => 'female '), 'Three' => array ('name' => '王', 'age' => '32 ', 'Sex' => 'male'), 'Four '=> array ('name' => 'Zhao Liu', 'age' => '12 ', 'Sex' => 'female '); foreach ($ arr as $ key => $ value) {foreach ($ value as $ key2 => $ value2) {echo $ value2;} echo"
";}?>