How to remove duplicate values from a multidimensional array?

假装没事ソ 提交于 2019-11-29 17:21:06

You don't need any custom function calls for this task. Just use array_column() to assign new temporary keys to each subarray, then reindex the subarrays and print to screen. *this does overwrite earlier occurrences with later ones.

Code: (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')));

Output:

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

If you want to keep the first occurrences and ignore all subsequent duplicates, this will do:

Code: (Demo) (* this keeps the first occurrences)

$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

Output:

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

If you don't mind losing the order of your subarrays, you can use array_reverse() with array_column() to retain the first occurrences using temporary keys, then use array_values() to re-index:

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

Result:

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

Took me a while, but this should work (explanation in comments):

<?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>';

?>

Function was taken from this the answer to this question: Get the keys for duplicate values in an array

Output:

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
        )

)

Tested and working example.

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

Will output:

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

taken from here http://php.net/manual/en/function.array-unique.php in the user contributed notes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!