33

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...

1

8 Answers 8

75

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);
4
  • 2
    I wonder why this doesn't work array_unique(array_map(function ($i) { return $i[$id]; }, $holder)); Is there a way to use variable instead of predefined key name?
    – Ilia Ross
    Aug 30, 2014 at 13:44
  • @deceze Ah, alright, it's inside of anonymous function. Thanks!
    – Ilia Ross
    Aug 30, 2014 at 15:19
  • This works but is inefficient. Why add a bunch of elements and remove them immediately? Just loop over the array and filter one by one! I'm adding my own answer. Feb 11, 2015 at 23:37
  • 1
    @Jorge Yes, this could be made more efficient by looping over the array once, putting all its elements into a new array as keys, then extracting just the keys. However, there's a tradeoff between readability, maintainability and performance, and I think this solution strikes a decent balance which will be just fine for average sized arrays.
    – deceze
    Feb 11, 2015 at 23:48
42

In php >= 5.5 you can use array_column:

array_unique(array_column($holder, 'pid'));
0
4

try this

foreach($arr as $key => $val) {
    $new_arr[] = $val['pid'];
}
$uniq_arr = array_unique($new_arr);
4

Just iterate over it and apply an array_unique on the result:

foreach($holder as $yourValues){
    $pids[] = $yourValues['pid'];
}
$yourUniquePids = array_unique($pids);
4

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.

1
  • in_array is essentially looping over the array again and again, so you've got two nested loops. That's inefficient in its own way.
    – deceze
    Feb 11, 2015 at 23:45
1
$uniquearray = [];
    for($i=0;$i<count($assocarray);$i++){
        if(!in_array($assocarray[$i]['KEY'],$uniquearray)){
            $uniquearray[$i]= $assocarray[$i]['KEY'];
        }
}
1
  • 6
    Can you provide a brief explanation in addition to your code?
    – Sam Chats
    Feb 3, 2020 at 13:36
0

Hi Please try code given below for get unique values and then sort that values

<?php

$sort_arr = unique_sort($holder, 'pid');
echo "<pre>";
print_r($sort_arr);
echo"</pre>";

/*function for get unique value then sort them*/

function unique_sort($arrs, $id) {
    $unique_arr = array();
    foreach ($arrs AS $arr) {

        if (!in_array($arr[$id], $unique_arr)) {
            $unique_arr[] = $arr[$id];
        }
    }
    sort($unique_arr);
    return $unique_arr;
}

thanks

-1

fo my situation i use this method

//get some data from db to array
while ($row = mysqli_fetch_assoc($query)){
   $data[] = $row;
}

//display unique value
echo "<table>";
$uniq = array();
foreach ($data as $row) {
    if(!in_array($row['path'], $uniq)) {
        $uniq[] = $row['path'];
        echo "<tr>";
        echo "<td>";
        echo $row['path'];
        echo "</td>";
        echo "<td>";
        echo $row['relationship_id'];
        echo "</td>";
        echo "</tr>";
    }
}
echo "</table>";
1
  • 2
    I don't see how that respond to the question: wich is getting unique value in a multidimensional array
    – goto
    Dec 30, 2016 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.