問題描述
我在溢出和谷歌上做了很多環(huán)顧四周,但沒有一個結果適用于我的具體情況.
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 的占位符數(shù)組,值如下:
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
)
)
我試圖從這個多維數(shù)組中提取不同/唯一的值.我想要的最終結果是一個包含 (13,121) 的變量,或者(最好)一個數(shù)組,如下所示:大批([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 )
我再次嘗試序列化等,但不太明白在每個數(shù)組中使用單個鍵操作時它是如何工作的.
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
值提取到自己的數(shù)組中,通過 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);
這篇關于如何在多維數(shù)組中獲取唯一值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!