I have done a lot of looking around on the overflow, and on google, but none of the results works for my specific case.
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
]
]
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...
asked May 2, 2012 at 6:01
MaurerPowerMaurerPower
1,9765 gold badges25 silver badges47 bronze badges
1
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]];
The same thing in longhand:
$pids = array[];
foreach [$holder as $h] {
$pids[] = $h['pid'];
}
$uniquePids = array_unique[$pids];
answered May 2, 2012 at 6:06
4
In php >= 5.5 you can use array_column:
array_unique[array_column[$holder, 'pid']];
answered May 25, 2015 at 14:48
1
try this
foreach[$arr as $key => $val] {
$new_arr[] = $val['pid'];
}
$uniq_arr = array_unique[$new_arr];
answered May 2, 2012 at 6:08
NauphalNauphal
6,1384 gold badges27 silver badges43 bronze badges
Just iterate over it and apply an array_unique
on the result:
foreach[$holder as $yourValues]{
$pids[] = $yourValues['pid'];
}
$yourUniquePids = array_unique[$pids];
answered May 2, 2012 at 6:08
konsolenfreddykonsolenfreddy
9,4821 gold badge24 silver badges36 bronze badges
Assuming your array is called $holder:
$unique = array[];
foreach[ $holder as $h ]
if[ ! in_array[$h, $unique ] ]
$unique[] = $h;
is a slightly more efficient way than via array_unique, I believe. May be the same.
answered Feb 11, 2015 at 23:43
1
$uniquearray = [];
for[$i=0;$i