2017-08-10 110 views
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,…} 

我想USER_ID检查重复。

我已经尝试了以下解决方案,但都没有按要求工作。

$input = array_unique($res, SORT_REGULAR);

$input = array_map("unserialize", array_unique(array_map("serialize", $res)));

我也试过以下。

$results = array(); 

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

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

但仍存在重复数据。

+0

哇我看到了投票。任何具体原因? – Ironic

+0

我也没有得到那种低调。猜猜这是在这个小时询问的风险。许多用户发送垃圾邮件随机票的徽章或其他东西.. – icecub

+0

@icecub我试图正确地解释问题。还搜索了答案。还提到了我曾经尝试过的。但仍然下降。 – Ironic

回答

3

您不需要任何自定义函数调用此任务。只需使用array_column()为每个子阵列分配新的临时密钥,然后重新索引子阵列并打印到屏幕。 *这确实会覆盖较早发生的事件。

代码:(Demo

$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] 
]; 
var_export(array_values(array_column($array,NULL,'user_id'))); 

输出:

array (
    0 => 
    array (
    'user_id' => 82, 
    'ac_type' => 2, 
), 
    1 => 
    array (
    'user_id' => 80, 
    'ac_type' => 6, 
), 
    2 => 
    array (
    'user_id' => 76, 
    'ac_type' => 1, 
), 
) 

如果你想保持第一事件,而忽略所有后续的重复,这会做:

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

$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, 
), 
) 

如果你不介意丢失子阵列的顺序,你可以使用array_reverse()array_column()使用临时键保留第一个分身,然后用array_values()重新索引:

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, 
), 
) 
*/ 
+0

我不确定这是不是他想要的。他想仅根据user_id删除重复项。这将保留_last_副本并删除所有以前的副本。我想他想保留第一场比赛,并删除所有重复? – icecub

+0

你是对的。如果这对实际应用有所不同,我将需要对其进行重新修改以保留第一次出现。 – mickmackusa

+0

无论如何,从我+1。我已经从你的口水中学到了一些东西。谢谢:) – icecub

1

我花了一段时间,但这应该工作(在评论中说明):

<?php 

/* Example array */ 
$result = 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 
     ), 
    3 => array(
     "user_id" => 82, 
     "ac_type" => 2 
     ), 
    4 => array(
     "user_id" => 80, 
     "ac_type" => 2 
     ) 
); 

/* Function to get the keys of duplicate values */ 
function get_keys_for_duplicate_values($my_arr, $clean = false) { 
    if ($clean) { 
     return array_unique($my_arr); 
    } 

    $dups = $new_arr = array(); 
    foreach ($my_arr as $key => $val) { 
     if (!isset($new_arr[$val])) { 
     $new_arr[$val] = $key; 
     } else { 
     if (isset($dups[$val])) { 
      $dups[$val][] = $key; 
     } else { 
      //$dups[$val] = array($key); 
      $dups[] = $key; 
      // Comment out the previous line, and uncomment the following line to 
      // include the initial key in the dups array. 
      // $dups[$val] = array($new_arr[$val], $key); 
     } 
     } 
    } 
    return $dups; 
} 

/* Create a new array with only the user_id values in it */ 
$userids = array_combine(array_keys($result), array_column($result, "user_id")); 

/* Search for duplicate values in the newly created array and return their keys */ 
$dubs = get_keys_for_duplicate_values($userids); 

/* Unset all the duplicate keys from the original array */ 
foreach($dubs as $key){ 
    unset($result[$key]); 
} 

/* Re-arrange the original array keys */ 
$result = array_values($result); 

echo '<pre>'; 
print_r($result); 
echo '</pre>'; 

?> 

功能,从这个答案带到了这个问题:Get the keys for duplicate values in an array

输出:

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 
     ) 

) 
+0

嗨,谢谢你的答案。投了票。 – Ironic

1

经过测试和工作示例。

<?php 

$details = 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'), '3'=>array('user_id'=>'82', 'ac_type'=>'1'), '4'=>array('user_id'=>'80', 'ac_type'=>'5')); 

function unique_multidim_array($array, $key) { 
$temp_array = array(); 
$i = 0; 
$key_array = array(); 

foreach($array as $val) { 
    if (!in_array($val[$key], $key_array)) { 
     $key_array[$i] = $val[$key]; 
     $temp_array[$i] = $val; 
    } 
    $i++; 
    } 
    return $temp_array; 
} 
?> 

<?php 
$details = unique_multidim_array($details,'user_id'); 
?> 

<pre> 

<?php print_r($details); ?> 

</pre> 

将输出:

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

从这里http://php.net/manual/en/function.array-unique.php在用户所贡献的笔记。

+0

感谢您的回答。投了票。 – Ironic

2
$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) 
    } 
} 
+0

感谢您的回答。你们是最好的。 – Ironic