PHP removes repeated elements in the array and sorts the elements by pressing the name. 1. purpose of this function: Remove repeated elements in the array and sort the functionassoc_unique ($ arr, $ key) {$ tmp_arrarray (); foreach ($ arras $ k $ v) {if (in_array ($ v [$ key], $ t 1. purpose of this function: Remove repeated elements in the array and sort by key names
Function assoc_unique ($ arr, $ key ){
$ Tmp_arr = array ();
Foreach ($ arr as $ k =>$ v ){
If (in_array ($ v [$ key], $ tmp_arr )){
Unset ($ arr [$ k]);
} Else {
$ Tmp_arr [] = $ v [$ key];
}
}
Sort ($ arr );
Return $ arr;
}
Example:
$ Aa = array (
Array ('id' => 123, 'name' => 'Zhang San '),
Array ('id' => 123, 'name' => 'Lily '),
Array ('id' => 124, 'name' => 'Wang Wu '),
Array ('id' => 125, 'name' => 'Zhao Liu '),
Array ('id' => 126, 'name' => 'Zhao Liu ')
);
$ Key = 'id ';
Assoc_unique (& $ aa, $ key );
Print_r ($ aa );
Using function assoc_unique ($ arr, $ key) {$ tmp_arr = array (); foreach ($ arr as $ k = $ v) {if (in_array ($ v [$ key], $ t...