English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Method of removing duplicate items from a two-dimensional PHP array while retaining all keys and values

This article illustrates the method of removing duplicates from a PHP two-dimensional array. Shared for everyone's reference, as follows:

For the following two-dimensional array, the requirement is to remove duplicates:

$arr = array(
     '0' => array(
          'name' => 'james',
          'age' =>30,
          ),
     "1=> array(
          'name' => 'susu',
          'age' =>26,
          ),
     "2=> array(
          'name' => 'james',
          'age' =>30,
          ),
     'new' => array(
          'name' => 'kube',
          'age' =>37,
          ),
     'list' => array(
          'name' => 'kube',
          'age' =>27,
          ),
     );

1In a two-dimensional array, the values in the one-dimensional arrays inside cannot be completely the same, remove the repeated items:

Code as follows:

<?php
$arr = array(
     '0' => array(
          'name' => 'james',
          'age' =>30,
          ),
     "1=> array(
          'name' => 'susu',
          'age' =>26,
          ),
      "2=> array(
          'name' => 'james',
          'age' =>30,
          ),
      'new' => array(
          'name' => 'kube',
          'age' =>37,
          ),
      'list' => array(
          'name' => 'kube',
          'age' =>27,
          ),
      );
printf("Before tranform the array:<br>");  //Output the original array
print_r($arr);
echo "<br/>";
function more_array_unique($arr = array()) {
  foreach ($arr[0] as $k => $v) {
    $arr_inner_key[] = $k;  //Firstly, record the key-value pairs of the inner arrays in the two-dimensional array in a one-dimensional array
  }
  foreach ($arr as $k => $v) {
    $v = join(",", $v);  //Dimension reduction: implode() can also be used
    $temp[$k] = $v;   //Preserve the original key-value pairs: $temp[] is without preserving original key-value pairs
  }
  printf("After split the array:<br>");
  print_r($temp);  //Output the split array
  echo "<br"/>";
  $temp = array_unique($temp);  //Remove duplicates: remove repeated strings
  foreach ($temp as $k => $v) {
    $a = explode(",", $v);  //Reconstruction after splitting, e.g., Array( [0] => james [1] => 30 )
    $arr_after[$k] = array_combine($arr_inner_key, $a); //Recombine the original keys and values
  }
  //ksort($arr_after);//Sorting required: ksort sorts the array while preserving the original key-value pairs (keys), sort does not preserve keys
  return $arr_after;
}
$arr_new = more_array_unique($arr); //Call the duplicate removal function
printf("Duplicate removal of the array:<br>");
print_r($arr_new);
echo "<br/>";
?>

Output result:

Before tranform the array:  //Original array
Array ( [0] => Array ( [name] => james [age] => 30 ) [1] => Array ([name] => susu [age] => 26 ) [2] => Array ( [name] => james [age]=> 30 ) [new] => Array ( [name] => kube [age] => 37 ) [list] =>Array ( [name] => kube [age] => 27 ) )
After split the array: //Array after split
Array ( [0] => james,30 [1] => susu,26 [2] => james,30 [new] =>kube,37 [list] => kube,27 )
Duplicate removal of the array: //Array after removal of duplicates
Array ( [0] => Array ( [name] => james [age] => 30 ) [1] => Array ([name] => susu [age] => 26 ) [new] => Array ( [name] => kube [age]=> 37 ) [list] => Array ( [name] => kube [age] => 27 ) )

2、The one-dimensional array inside the two-dimensional array cannot have the same key value due to a certain key, delete duplicate items:

Remove duplicates for a specific key value

<?php
$arr= array(……); //Same as the above two-dimensional array
function second_array_unique_bykey($arr, $key){
  $tmp_arr = array();
  foreach($arr as $k => $v)
  {
    if(in_array($v[$key], $tmp_arr))  //Search if $v[$key] exists in the $tmp_arr array, if it exists, return true
    {
      unset($arr[$k]); //Destroy a variable, if the same value exists in $tmp_arr, delete the value
    }
    else {
      $tmp_arr[$k] = $v[$key]; //Place different values in this array to save
    }
  }
  //ksort($arr); //ksort function sorts the array (retains the original key value key) sort does not retain key value
  return $arr;
  }
  $key ='name';
  $arr_key = second_array_unique_bykey($arr,$key);
  printf("As for the given key->%s:<br>"
  print_r($arr_key);
  echo "<br/>";
?>

Output result:

As for the given key->name:
Array ( [0] => Array ( [name] => james [age] => 30 ) [1] => Array ([name] => susu [age] => 26 ) [new] => Array ( [name] => kube [age]=> 37 ) )

PS: This site also has two relatively simple and practical online text duplicate removal tools, which are recommended for everyone to use:

Online Duplicate Item Removal Tool:
http://tools.jb51.net/code/quchong

Online Text Duplicate Removal Tool:
http://tools.jb51.net/aideddesign/txt_quchong

More about PHP-related content, readers who are interested can check the special topic of this site: 'PHP Array (Array) Operation Skills Summary', 'PHP Common Traversal Algorithm and Skill Summary', 'PHP String (string) Usage Summary', 'PHP Common Function and Skill Summary', 'PHP Error and Exception Handling Method Summary', 'PHP Basic Syntax Tutorial', 'PHP Object-Oriented Program Design Tutorial', 'php+MySQL Database Operation Tutorial and PHP Common Database Operation Skills Summary

I hope the description in this article will be helpful to everyone's PHP program design.

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously, and this website does not own the copyright. It has not been manually edited and does not assume relevant legal liabilities. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (when sending an email, please replace # with @ to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)

You may also like