Small example of removing duplicates from php arrays
- 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;
- }
-
- $ 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 = 'name ';
- Assoc_unique (& $ aa, $ key );
- Print_r ($ aa );
- ?>
-
Articles you may be interested in: php: how to create an array: Introduction to php: how to use array_unique to determine whether the same value exists in the array. a small example of php array and loop. php: how to find a specific value in a multi-dimensional array. enhanced array_unique function (two-dimensional array supported) php multi-dimensional array sorting example of php multi-dimensional array sorting php array callback filter function array_filter () application instance php extract the function of variable from the array and application instance to the php code of multi-dimensional array sorted by a certain value randomly extract some elements from the array php code |