PHP two-dimensional array deduplication method (preserving individual key values while removing duplicates)--uniqueness of two-dimensional arrays
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:
Array (' name ' = ' James ', ' age ' =>30,), ' 1 ' =>array ( ' Name ' = ' susu ', ' age ' =>26, ' 2 ' =>array ( ' Name ' = ' James ', ' Age ' =>30, ' the ' New ' =>array ( ' Name ' = ' kube ', ' age ' =>37,), ' list ' =>array ( ' Name ' = ' kube ', ' age ' =>27,),;p rintf (before tranform the array:); Output the original array print_r ($arr), Echo, 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; Keep the original key value $temp [] that is, do not retain the original key value} printf (after split the array:); Print_r ($temp); Output split array echo; $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:); Print_r ($arr _new), echo;? >
Output Result:
Before tranform the array://original arrays
Array ([0] = = Array ([name] = James [age] = +) [1] = = Array ([name] = + Susu [age] = +) [2] => ; Array ([name] = James [age]=>) [new] + = Array ([name] = Kube [age] + Notoginseng) [list] =>array ([Nam E] = kube [age] = 27))
After split the array://split arrays
Array ([0] = james,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 */
$v) { if (In_array ($v [$key], $tmp _arr)) //Search $v[$key] is present in the $tmp_arr array if there is a return of 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:, $key); Print_r ($arr _key); echo;? >
Output Result:
As for the given Key->name:
Array ([0] = = Array ([name] = James [age] = +) [1] = = Array ([name] = + Susu [age] = +) [New] =& Gt Array ([name] = kube [age]=> 37))
http://www.bkjia.com/PHPjc/1047174.html www.bkjia.com true http://www.bkjia.com/PHPjc/1047174.html techarticle php Two-dimensional array deduplication method (to retain individual key values while removing duplicates)--two-dimensional array uniqueness for the following two-dimensional array, it is required to go to the same weight: $arr = Array (0= ...