Home >Backend Development >PHP Tutorial >PHP gets the value set of a column in a two-dimensional array, _PHP tutorial
PHP is still relatively commonly used, so I studied PHP two-dimensional arrays. When processing PHP arrays, there is a particularly frequent need, such as the following two-dimensional array:
$arr = array( 1=>array( 'id' => 5, 'name' => '张三' ), 2=>array( 'id' => 6, 'name' => '李四' ) );
The purpose is to get the set whose key is name and get this result:
$arr2 = array( 0=>'张三', 1=>'李四' );
Here are several methods:
1: The simplest, foreach traverses the array:
foreach ($arr as $key => $value) { $arr2[] = $value['name']; }
2: A slightly smaller code, using the array_map php method:
$arr2 = array_map('array_shift',$arr);
means to remove the value at the beginning of each value of the $arr array, and return the value that was removed from each value. Note that the key of the new array $arr2 is still the original array $arr. The key
2.1: Based on method 2, you can use your imagination a little bit. If you need to get the beginning column or the end column of each item of the two-dimensional array, you can also do this:
$arr2 = array_map('reset',$arr); $arr2 = 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 imagination space of this method (for other array value operations) is still quite large:
$arr2 = array_reduce($arr, create_function('$result, $v', '$result[] = $v["name"];return $result;'));
The array_reduce method uses a callback function to iteratively operate on the values of the array, and create_function is used to make a callback to an anonymous method. The parameter $result of this anonymous method is the value generated by the previous iteration, and $v is the current value. , the internal implementation is to obtain the "name" value of each item in the array $arr and push it to the new $result array;
4: This ultimate method is so cool. It can be done in one method and is very flexible:
$arr2 = array_column($arr, 'name');
The second parameter is the key name of the column you want to get. Isn’t it very convenient? However, this method has a limitation, that is, the PHP version must be >= 5.5.0. This method is still used in old projects. You have to think about it
PS: Several ways to traverse two-dimensional arrays in php
<?php //使用for循环遍历 $arr2=array(array("张三","20","男"),array("李四","25","男"),array("王五","19","女"),array("赵六","25","女")); echo "<table border=2 bordercolor=red><tr><td>姓名</td><td>年龄</td& gt;<td>性别</td></tr>"; for($i=0;$i<4;$i++){ echo "<tr>"; for($j=0;$j<3;$j++){ echo "<td>"; echo $arr2[$i][$j]; echo "</td>"; } echo "</tr>"; echo "<br>"; } echo "</table>"; ?> //使用foreach遍历 <?php $arr = array('one'=>array('name'=>'张三','age'=>'23','sex'=>'男'), 'two'=>array('name'=>'李四','age'=>'43','sex'=>'女'), 'three'=>array('name'=>'王五','age'=>'32','sex'=>'男'), 'four'=>array('name'=>'赵六','age'=>'12','sex'=>'女')); foreach($arr as $k=>$val){ echo $val['name'].$val['age'].$val['sex']."<br>"; } echo "<p>"; ?> <?php $arr = array('one'=>array('name'=>'张三','age'=>'23','sex'=>'男'), 'two'=>array('name'=>'李四','age'=>'43','sex'=>'女'), 'three'=>array('name'=>'王五','age'=>'32','sex'=>'男'), 'four'=>array('name'=>'赵六','age'=>'12','sex'=>'女')); foreach($arr as $key=>$value){ foreach($value as $key2=>$value2){ echo $value2; } echo "<br>"; } ?>