PHP two-dimensional array deduplication method (retain each key value while removing repeated items)-uniqueness of the two-dimensional array. PHP two-dimensional array deduplication method (retain each key value while removing repeated items) -- The uniqueness of the two-dimensional array requires deduplication for the following two-dimensional array: $ arrarray (0 PHP two-dimensional array deduplication method (retain each key value while removing repeated items) -- Uniqueness of two-dimensional array
Deduplicate the following two-dimensional array:
$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 the one-dimensional array inside the two-dimensional array cannot be identical. delete the repeated items:
The code is as follows:
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 :); // 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 record the key value of the inner array in the two-dimensional array in the one-dimensional array} Foreach ($ arras $ k =>$ v) {$ v = join (, $ v); // use implode () for dimensionality reduction () $ temp [$ k] = $ v; // retain the original key value $ temp [], that is, do not retain the original key value} printf (After split the array :); print_r ($ temp); // output the split array echo; $ temp = array_unique ($ temp); // deduplication: remove the repeated string foreach ($ tempas $ k => $ v) {$ a = explode (, $ v); // The restructured string after splitting, for example: array ([0] => james [1] => 30) $ arr_after [$ k] = array_combine ($ arr_inner_key, $ ); // re-merge the original key and value} // ksort ($ arr_after); // sort as needed: ks Ort sorts the array (retain the original key value key), sort does not retain the key value return $ arr_after;} $ arr_new = more_array_unique ($ arr ); // call the deduplicated function printf (Duplicate removal of the array :); print_r ($ arr_new); echo;?>
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 thearray: // deduplicated array
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. because a key value of a one-dimensional array cannot be the same, delete the repeated items:
/* Deduplicate a key value */
$ V) {if (in_array ($ v [$ key], $ tmp_arr) // search for $ v [$ key] in the $ tmp_arr array, if yes, true {unset ($ arr [$ k]) is returned. // destroy a variable. if $ tmp_arr already has the same value, delete the variable.} else {$ tmp_arr [$ k] = $ v [$ key]; // store different values in this array} // ksort ($ arr); // The ksort function sorts the array (retain the original key value key) 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] => 30) [1] => Array ([name] => susu [age] => 26) [new] => Array ([name] => kube [age] => 37 ))
Distinct (preserve each key value while removing repeated items) -- The uniqueness of a two-dimensional array requires deduplication for the following two-dimensional array: $ arr = array (0 =...