This paper mainly introduces the implementation of PHP two-dimensional array to remove duplicates of the method, combined with examples of PHP to retain the various keys to remove duplicates of the relevant operation skills, the need for friends can refer to, hope to help everyone.
For the following two-dimensional arrays, it is required to be de-weighed:
$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, ) ;
1. The values in a one-dimensional array inside a two-dimensional array cannot be identical, removing the duplicates:
The code is 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,),);p rintf ("before tranform the Array:<b R> "); 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; First, the key values of the inner arrays in the two-dimensional array are recorded in a one-dimensional array} foreach ($arras $k = = $v) {$v =join (",", $v); dimensionality reduction with implode () also line $temp [$k] = $v; Retain the original key value $temp [] that is, do not retain the original key value} printf ("After split the Array:<br>"); Print_r ($temp); Output split array echo "<br/>"; $temp =array_unique ($temp); Deduplication: Remove the duplicate string foreach ($tempas $k + = $v) {$a = explode (",", $v); After splitting the reorganization such as:Array ([0] = James [1] = +) $arr _after[$k]= array_combine ($arr _inner_key, $a); Re-merge the original key with the value}//ksort ($arr _after);//Sort as required: Ksort to sort the array (keep the original key value key), sort to not retain the key value return$arr_after;} $arr _new = More_array_unique ($arr); Call the Remove-weight function printf ("Duplicate removal of the array:<br>");p Rint_r ($arr _new); echo "<br/>";? >
Output Result:
Before tranform the array: //original arrays ([0] = = Array ([name] = James [age] +) [1] = = Array ([name] = Susu [age] = [2] = = Array ([name] = James [age]=>) [new] + = Array ([name] + kube [a GE] [=>array] [list] ([name] = Kube [age] +)) after split the array://split arrays ([0] = Jam es,30 [1] = susu,26 [2] = james,30 [new] =>kube,37 [list] = kube,27) Duplicate removal of TheArray://de-weight after array Array ([0] = = Array ([name] = James [age] = +) [1] = = Array ([name] = + Susu [age] = +) [New] =& Gt Array ([name] = kube [age]=> notoginseng) [List] = Array ([name] = Kube [age] = 27))
2. A one-dimensional array inside a two-dimensional array can delete duplicates because one of the key values cannot be the same:
For a certain key value to go to the weight
<?php$arr= Array (...); The two-dimensional array functionsecond_array_unique_bykey ($arr, $key) { $tmp _arr = Array (); foreach ($arr as $k + $v) { if (In_array ($v [$key], $tmp _arr) //Search $v[$key] is present in the $tmp_arr array, if present returns True { unset ($arr [$k]);//Destroy a variable if the same value already exists in $tmp_arr, delete the value } else { $tmp _arr[$k] = $v [$key]; Place a different value in the array Save } } //ksort ($arr);//ksort function Array (retains the original key value key) sort to not retain the key value return $arr; } $key = ' name '; $arr _key = Second_array_unique_bykey ($arr, $key); printf ("As for the Givenkey->%s:<br>", $key); Print_r ($arr _key); echo "<br/>";? >
Output Result:
As for the given Key->name:array ([0] = = Array ([name] + James [age] = +) [1] = = Array ([name] = = Susu [age] = [new] = Array ([name] = kube [age]=> 37))