PHP is still more commonly used, so I studied the PHP two-dimensional array. When dealing with an array of PHP, there is a need to be particularly frequent, the following two-dimensional array:
$arr = Array (
1=>array (
' id ' => 5,
' name ' => ' John '
),
2=>array (
' id ' => 6,
' Name ' => ' Dick '
)
);
The goal is to take the collection of key name and get the result:
$arr 2 = Array (
0=> ' John ',
1=> ' Dick '
);
Here are a few ways:
1: The simplest, foreach traversal array:
foreach ($arr as $key => $value) {
$arr 2[] = $value [' name '];
}
2: Code with a little less code, using the Array_map this PHP method:
$arr 2 = array_map (' Array_shift ', $arr);
To move the value of each value of the $arr array out and return the value that was removed from each of the values being removed, notice that the key to the new array $arr2 is still the key of the original array $arr
2.1: On the basis of Method 2, you can open the brain hole a little bit, or you can do this if you need to get the beginning or end column of each item in a two-dimensional array:
$arr 2 = array_map (' Reset ', $arr);
$arr 2 = array_map (' End ', $arr);
Haha, it's also very convenient.
3: You can also use the Array_reduc E method, but the code is slightly more, but the imaginary space of this method (for other array value operations) is quite large:
$arr 2 = array_reduce ($arr, Create_function (' $result, $v ', ' $result [] = $v [' name '];return $result; ')];
The Array_reduce method uses the callback function to iterate over the values of the array, while create_function is used for an anonymous method to invoke the parameter of the anonymous method $result the value produced by the previous iteration, $v is the current value, and the internal implementation is both to get the array $ Arr The value of the "name" of each item and push to the new $result array;
4: Finally this ultimate method is really cool, one method is done, and very flexible:
$arr 2 = array_column ($arr, ' name ');
The second parameter for the column you want to get the key name, is not very convenient, but this method has a limitation, that is, PHP version must be >= 5.5.0, in the old project to use this method still have to weigh the HA
Several ways of traversing two-dimensional arrays in ps:php
<?php//Use for loop traversal $arr 2=array (Array ("John", "20", "male"), Array ("Dick", "25", "male"), Array ("Harry", "19", "female"), Array ("Zhao Liu", "25",
"female")); echo "<table border=2 bordercolor=red><tr><td> name </td><td> age </td& GT;<TD
> Sex </td></tr> ";
For ($i =0 $i <4; $i + +) {echo "<tr>" for ($j =0; $j <3; $j + +) {echo "<td>";
echo $arr 2[$i] [$j];
echo "</td>";
echo "</tr>";
echo "<br>"; echo "</table>";?>//using foreach traversal <?php $arr = Array (' One ' =>array (' name ' => ' John ', ' Age ' => ' 23 ', ' Sex ' => '), ' two ' =>array (' name ' => ' Dick ', ' age ' => ', ' sex ' => ' female '), ' three ' =>array (' name ' => ' Harry '),
' Age ' => ', ' sex ' => ' man ', ' Four ' =>array (' name ' => ' Zhao Liu ', ' age ' => ', ' sex ' => ' female ')); foreach ($arr as $k => $val) {echo $val [' name ']. $val [' age ']. $val [' sex ']. "
<br> "; echo "<p>";?> <?php $arr = Array (' One ' =>array (' name ' => ' John ', ' Age ' => ', ' sex ' => ' man '), ' two ' =& Gt;array (' name ' => ' Dick ', ' AGE ' => ', ' sex ' => ' female ', ' three ' =>array (' name ' => ' Harry ', ' Age ' => ', ' sex ' => ' man '), ' Four ' =>array ('
Name ' => ' Zhao Liu ', ' age ' => ', ' sex ' => ' female '); foreach ($arr as $key => $value) {foreach ($value as $key 2=> $value 2) {echo $value 2;} echo "<br>";}?>