: This article mainly introduces the PHP two-dimensional array deduplication method (retaining each key value while removing repeated items)-The uniqueness of the two-dimensional array, if you are interested in the PHP Tutorial, refer. 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; // record the key value of the inner array of 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); // outputs the split array echo"
"; $ Temp = array_unique ($ temp); // deduplication: remove the repeated string foreach ($ tempas $ k = >$ v) {$ a = explode (", ", $ v); // The restructured result after splitting, for example, Array ([0] => james [1] => 30) $ arr_after [$ k] = array_combine ($ arr_inner_key, $ a); // re-combine the original key and value} // ksort ($ arr_after ); // sort the array as needed: ksort 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 ))
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.
The preceding section describes the PHP two-dimensional array deduplication method (retained each key value while removing repeated items)-uniqueness of the two-dimensional array, including content, if you are interested in PHP tutorials.