search
Homephp教程php手册PHP获取二维数组中某一列的值集合,

PHP获取二维数组中某一列的值集合,

PHP还是比较常用的,于是我研究了一下PHP二维数组。在处理php数组的时候,有一种需求特别的频繁,如下二维数组:

$arr = array(
1=>array(
'id' => 5,
'name' => '张三'
),
2=>array(
'id' => 6,
'name' => '李四'
)
);

目的就是要取到key为name的集合,得到这个结果:

$arr2 = array(
0=>'张三',
1=>'李四'
);

这里有几种方法:

1:最简单的,foreach遍历数组:

foreach ($arr as $key => $value) {
$arr2[] = $value['name'];
}

2:代码量稍微少一点的代码,使用了 array_map 这个php方法:

$arr2 = array_map('array_shift',$arr);

意为把$arr数组的每一项值的开头的值移出,并返回被移出的每一项值中被移出的值,注意此时新数组$arr2的键仍是原数组$arr的键

2.1:在方法2的基础上,可以稍微开一下脑洞,如果需要获取的是二维数组的每一项的开头列或结尾列,也可以这样做:

$arr2 = array_map('reset',$arr);
$arr2 = array_map('end',$arr);

哈哈,也是很方便的

3:还可以使用 array_reduc e方法,不过代码略多,不过这个方法的想象空间(针对别的数组取值操作)还是蛮大的:

$arr2 = array_reduce($arr, create_function('$result, $v', '$result[] = $v["name"];return $result;'));

array_reduce方法用回调函数迭代地将对数组的值进行操作,而create_function用于一个匿名方法做回调用,这个匿名方法的参数$result为上一次迭代产生的值,$v是当前的值,内部实现既为获取到数组$arr每一项的”name”的值并push到新$result数组;

4:最后这个终极方法实在是太爽了,一个方法就搞定,而且非常灵活:

$arr2 = array_column($arr, 'name');

第二个参数为想要获取的那一列的键名,是不是很方便呢,不过这个方法有个限制,就是php版本必须 >= 5.5.0,在陈旧项目中使用这个方法还是得斟酌一下哈

PS: php中遍历二维数组的几种方式

<&#63;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>";
&#63;>
//使用foreach遍历
<&#63;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>";
&#63;>
<&#63;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>";
}
&#63;>

您可能感兴趣的文章:

  • php对二维数组按指定键值key排序示例代码
  • 一个PHP二维数组排序的函数分享
  • php二维数组转成字符串示例
  • PHP二维数组排序的3种方法和自定义函数分享
  • PHP 如何获取二维数组中某个key的集合
  • Thinkphp将二维数组变为标签适用的一维数组方法总结
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft