How to get unique value in multidimensional array(如何在多维数组中获取唯一值)
问题描述
我在溢出和谷歌上做了很多环顾四周,但没有一个结果适用于我的具体情况.
I have done a lot of looking around on the overflow, and on google, but none of the results works for my specific case.
我有一个名为 $holder 的占位符数组,值如下:
I have a placeholder array called $holder, values as follows:
Array (
[0] => Array (
[id] => 1
[pid] => 121
[uuid] => 1
)
[1] => Array (
[id] => 2
[pid] => 13
[uuid] => 1
)
[2] => Array (
[id] => 5
[pid] => 121
[uuid] => 1
)
)
我试图从这个多维数组中提取不同/唯一的值.我想要的最终结果是一个包含 (13,121) 的变量,或者(最好)一个数组,如下所示:大批([0] => 13[1] => 121)
I am trying to pull out distinct/unique values from this multidimensional array. The end result I would like is either a variable containing (13,121), or (preferrably) an array as follows: Array( [0] => 13 [1] => 121 )
我再次尝试序列化等,但不太明白在每个数组中使用单个键操作时它是如何工作的.
Again I've tried serializing and such, but don't quite understand how that works when operating with a single key in each array.
我尽量说清楚.我希望这是有道理的...
I tried to be as clear as possible. I hope it makes sense...
推荐答案
看起来很简单:将所有 pid
值提取到自己的数组中,通过 array_unique
运行:
Seems pretty simple: extract all pid
values into their own array, run it through array_unique
:
$uniquePids = array_unique(array_map(function ($i) { return $i['pid']; }, $holder));
同样的东西:
$pids = array();
foreach ($holder as $h) {
$pids[] = $h['pid'];
}
$uniquePids = array_unique($pids);
这篇关于如何在多维数组中获取唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在多维数组中获取唯一值
基础教程推荐
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01