Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从多维数组中删除重复值?_Php_Multidimensional Array_Duplicates_Filtering - Fatal编程技术网

Php 如何从多维数组中删除重复值?

Php 如何从多维数组中删除重复值?,php,multidimensional-array,duplicates,filtering,Php,Multidimensional Array,Duplicates,Filtering,我有来自两个单独mysql查询的数组数据。数组数据如下所示: 0 : {user_id: 82, ac_type: 1,…} 1 : {user_id: 80, ac_type: 5,…} 2 : {user_id: 76, ac_type: 1,…} 3 : {user_id: 82, ac_type: 1,…} 4 : {user_id: 80, ac_type: 5,…} 我想删除重复的数组项 因此,我的输出如下: 0 : {user_id: 82, ac_type: 1,…} 1 :

我有来自两个单独mysql查询的数组数据。数组数据如下所示:

0
:
{user_id: 82, ac_type: 1,…}
1
:
{user_id: 80, ac_type: 5,…}
2
:
{user_id: 76, ac_type: 1,…}
3
:
{user_id: 82, ac_type: 1,…}
4
:
{user_id: 80, ac_type: 5,…}
我想删除重复的数组项

因此,我的输出如下:

0
:
{user_id: 82, ac_type: 1,…}
1
:
{user_id: 80, ac_type: 5,…}
2
:
{user_id: 76, ac_type: 1,…}
我想按用户id检查副本

我尝试了以下解决方案,但都没有达到预期效果

$input=array\u unique($res,SORT\u REGULAR)

$input=array\u map(“非序列化”,array\u唯一(array\u map(“序列化”),$res))

我也试过了

$results = array();

foreach ($res as $k => $v) {
        $results[implode($v)] = $v;
}

$results = array_values($results);
print_r($results);

但仍然存在重复数据。

花了我一段时间,但这应该可以工作(注释中的解释):


测试和工作示例

Array
(
[0] => Array
    (
        [user_id] => 82
        [ac_type] => 1
    )

[1] => Array
    (
        [user_id] => 80
        [ac_type] => 5
    )

[2] => Array
    (
        [user_id] => 76
        [ac_type] => 1
    )
)

摘自用户提供的笔记。

此任务不需要任何自定义函数调用。只需使用
array\u column()*这会用以后的事件覆盖以前的事件

代码:()

输出:

array (
  0 => 
  array (
    'user_id' => 82,
    'ac_type' => 1,
  ),
  1 => 
  array (
    'user_id' => 80,
    'ac_type' => 5,
  ),
  2 => 
  array (
    'user_id' => 76,
    'ac_type' => 1,
  ),
)
$array = [
    ['user_id'=>82,'ac_type'=>1],
    ['user_id'=>80,'ac_type'=>5],
    ['user_id'=>76,'ac_type'=>1],
    ['user_id'=>82,'ac_type'=>2],
    ['user_id'=>80,'ac_type'=>6]
];

$array = array_reverse($array);

$v = array_reverse( 
    array_values( 
        array_combine( 
            array_column($array, 'user_id'),
            $array
        )
    )
);


echo '<pre>';
var_dump($v);

如果要保留第一个引用并忽略所有后续重复项,则可以执行以下操作:

代码:()(*保留第一次出现)

输出:

array (
  0 => 
  array (
    'user_id' => 82,
    'ac_type' => 1,
  ),
  1 => 
  array (
    'user_id' => 80,
    'ac_type' => 5,
  ),
  2 => 
  array (
    'user_id' => 76,
    'ac_type' => 1,
  ),
)
$array = [
    ['user_id'=>82,'ac_type'=>1],
    ['user_id'=>80,'ac_type'=>5],
    ['user_id'=>76,'ac_type'=>1],
    ['user_id'=>82,'ac_type'=>2],
    ['user_id'=>80,'ac_type'=>6]
];

$array = array_reverse($array);

$v = array_reverse( 
    array_values( 
        array_combine( 
            array_column($array, 'user_id'),
            $array
        )
    )
);


echo '<pre>';
var_dump($v);

如果您不介意丢失子数组的顺序,可以使用
array\u reverse()
array\u column()
使用临时键保留第一次出现的项,然后使用
array\u values()
重新索引:

array(3) {
  [0]=>
  array(2) {
    ["user_id"]=>
    int(76)
    ["ac_type"]=>
    int(1)
  }
  [1]=>
  array(2) {
    ["user_id"]=>
    int(82)
    ["ac_type"]=>
    int(1)
  }
  [2]=>
  array(2) {
    ["user_id"]=>
    int(80)
    ["ac_type"]=>
    int(5)
  }
}

哇,我看到了反对票。有什么特别的原因吗?我也没有得到反对票。我猜这是在这个时候问的风险。许多用户为了获得徽章或其他东西而随意投票。@icecub我试图正确地解释这个问题。也这样寻找答案。还提到了我试过的东西。但还是被否决了。我只能看到有人试图编辑你的问题标题。但就是这样。让我纠正一下,谢谢你。我从昨天开始就在努力解决这个问题,但到目前为止运气不好。我不确定这是否是他想要的。他只想根据用户id删除重复项。这将保留最后一个副本并删除所有以前的副本。我想他想保留第一个匹配项,然后删除所有重复项?你说得对。如果这对实际应用程序有影响,我需要对其进行返工以保留第一次出现的内容。无论如何,我需要+1。事实上,我从你的回答中学到了一些东西。谢谢:)谢谢你的回答。很好!非常有用+1.
$array=[
    ['user_id'=>82,'ac_type'=>1],
    ['user_id'=>80,'ac_type'=>5],
    ['user_id'=>76,'ac_type'=>1],
    ['user_id'=>82,'ac_type'=>2],
    ['user_id'=>80,'ac_type'=>6]
];

foreach($array as $a){
    if(!isset($result[$a['user_id']])){
        $result[$a['user_id']]=$a;      // only store first occurence of user_id
    }
}
var_export(array_values($result));  // re-index
array (
  0 => 
  array (
    'user_id' => 82,
    'ac_type' => 1,
  ),
  1 => 
  array (
    'user_id' => 80,
    'ac_type' => 5,
  ),
  2 => 
  array (
    'user_id' => 76,
    'ac_type' => 1,
  ),
)
var_export(array_values(array_column(array_reverse($array),NULL,'user_id')));
/*
array (
  0 => 
  array (
    'user_id' => 76,
    'ac_type' => 1,
  ),
  1 => 
  array (
    'user_id' => 82,
    'ac_type' => 1,
  ),
  2 => 
  array (
    'user_id' => 80,
    'ac_type' => 5,
  ),
)
*/
$array = [
    ['user_id'=>82,'ac_type'=>1],
    ['user_id'=>80,'ac_type'=>5],
    ['user_id'=>76,'ac_type'=>1],
    ['user_id'=>82,'ac_type'=>2],
    ['user_id'=>80,'ac_type'=>6]
];

$array = array_reverse($array);

$v = array_reverse( 
    array_values( 
        array_combine( 
            array_column($array, 'user_id'),
            $array
        )
    )
);


echo '<pre>';
var_dump($v);
array(3) {
  [0]=>
  array(2) {
    ["user_id"]=>
    int(76)
    ["ac_type"]=>
    int(1)
  }
  [1]=>
  array(2) {
    ["user_id"]=>
    int(82)
    ["ac_type"]=>
    int(1)
  }
  [2]=>
  array(2) {
    ["user_id"]=>
    int(80)
    ["ac_type"]=>
    int(5)
  }
}