In php, array_unique removes repeated arrays from two-dimensional arrays. In php, array_unique is used to remove duplicate values of list-level arrays. For more information, see. The code is as follows: Copy the code? Php $ inputarray (agreen, red, bgre): array_unique in php is used to remove duplicate values of list-level arrays. For more information, see.
The code is as follows: |
|
$ Input = array ("a" => "green", "", "red", "B" => "green", "", "blue ", "red", "c" => "witer", "hello", "witer "); // $ Result = array_unique ($ input); // remove duplicate elements $ Result = a_array_unique ($ input );?? // Only leaves a single element Foreach ($ result as $ aa) { Echo $ aa ." "; } Function multi_unique ($ array ){ Foreach ($ array as $ k => $ na) $ New [$ k] = serialize ($ na ); $ Uniq = array_unique ($ new ); Foreach ($ uniq as $ k => $ ser) $ New1 [$ k] = unserialize ($ ser ); Return ($ new1 ); } Function a_array_unique ($ array) // write better { $ Out = array (); Foreach ($ array as $ key => $ value ){ If (! In_array ($ value, $ out )) { $ Out [$ key] = $ value; } } Return $ out; } ?> |
The array_unique function is only applicable to one-dimensional arrays and does not apply to multi-dimensional arrays. However, we can use the traversal data and then perform operations to remove repeated arrays.
The code is as follows: |
|
Function unique_arr ($ array2D, $ stkeep = false, $ ndformat = true) { // Determine whether the primary array key is retained (the primary array key can be non-numeric) If ($ stkeep) $ stArr = array_keys ($ array2D ); // Determine whether the secondary array key is retained (all secondary array keys must be the same) If ($ ndformat) $ ndArr = array_keys (end ($ array2D )); // Dimensionality reduction. you can also use implode to convert a one-dimensional array to a string connected with commas (,). Foreach ($ array2D as $ v ){ $ V = join (",", $ v ); $ Temp [] = $ v; } // Remove the duplicate string, that is, the duplicate one-dimensional array. $ Temp = array_unique ($ temp ); // Re-assemble the split array Foreach ($ temp as $ k => $ v) { If ($ stkeep) $ k = $ stArr [$ k]; If ($ ndformat) { $ TempArr = explode (",", $ v ); Foreach ($ tempArr as $ ndkey => $ ndval) $ output [$ k] [$ ndArr [$ ndkey] = $ ndval; } Else $ output [$ k] = explode (",", $ v ); } Return $ output; } $ Array2D = array ('first' => array ('title' => '000000', 'date' => '000000 ′), 'second' => array ('title' => '123', 'date' => '123 ′), 'third' => array ('title' => '000000', 'date' => '000000 ′)); Print_r ($ array2D ); Print_r (unique_arr ($ array2D, true )); |
Bytes. The code is as follows? Php $ input = array (a = green, red, B = gre...