PHP two-dimensional array to re-divided into the case, one is a two-dimensional array inside a one-dimensional array of values can not be exactly the same, delete the duplicate items, the other is a two-dimensional array inside a one-dimensional array because a key value cannot be the same, delete its duplicates.
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:<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; 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:<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); Called to remove the 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] = +) [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://Array after splitting
Array ([0] = james,30 [1] = susu,26 [2] = = james,30 [new] =>kube,37 [list] = kube,27)
Duplicate Removal of TheArray: //go to heavy 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] =& Gt Array ([name] = kube [age]=> 37))
Related recommendations:
A method of merging and de-weight of one-dimensional arrays in PHP