如何在多维数组中获取唯一值 [英] How to get unique value in multidimensional array

查看:35
本文介绍了如何在多维数组中获取唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在溢出和谷歌上做了很多环顾四周,但没有一个结果适用于我的具体情况.

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);

这篇关于如何在多维数组中获取唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆