php 如何在多维数组中获取唯一值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10408482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:05:51  来源:igfitidea点击:

How to get unique value in multidimensional array

phpmultidimensional-arrayunique-values

提问by MaurerPower

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:

我有一个名为 $holder 的占位符数组,值如下:

    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 )

我试图从这个多维数组中提取不同/唯一的值。我想要的最终结果是一个包含 (13,121) 的变量,或者(最好)一个数组,如下所示: 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...

我尽量说清楚。我希望这是有道理的...

回答by deceze

Seems pretty simple: extract all pidvalues into their own array, run it through array_unique:

看起来很简单:将所有pid值提取到他们自己的数组中,运行它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);

回答by Dondi Imperial

In php >= 5.5 you can use array_column:

在 php >= 5.5 中,您可以使用array_column

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

回答by Jorge Orpinel

Assuming your array is called $holder:

假设您的数组称为 $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.

我相信是一种比通过 array_unique 更有效的方法。可能是一样的。

回答by Nauphal

try this

尝试这个

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

回答by konsolenfreddy

Just iterate over it and apply an array_uniqueon the result:

只需迭代它并对array_unique结果应用一个:

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

回答by Er. Anurag Jain

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

谢谢

回答by Anti logic

$uniquearray = [];
    for($i=0;$i<count($assocarray);$i++){
        if(!in_array($assocarray[$i]['KEY'],$uniquearray)){
            $uniquearray[$i]= $assocarray[$i]['KEY'];
        }
}

回答by azwart

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