Small example of removing duplicates from php arrays

Source: Internet
Author: User
Small example of removing duplicates from php arrays

  1. Function assoc_unique ($ arr, $ key ){
  2. $ Tmp_arr = array ();
  3. Foreach ($ arr as $ k =>$ v ){
  4. If (in_array ($ v [$ key], $ tmp_arr )){
  5. Unset ($ arr [$ k]);
  6. } Else {
  7. $ Tmp_arr [] = $ v [$ key];
  8. }
  9. }
  10. Sort ($ arr );
  11. Return $ arr;
  12. }
  13. $ Aa = array (
  14. Array ('id' => 123, 'name' => 'Zhang San '),
  15. Array ('id' => 123, 'name' => 'Lily '),
  16. Array ('id' => 124, 'name' => 'Wang Wu '),
  17. Array ('id' => 125, 'name' => 'Zhao Liu '),
  18. Array ('id' => 126, 'name' => 'Zhao Liu ')
  19. );
  20. $ Key = 'name ';
  21. Assoc_unique (& $ aa, $ key );
  22. Print_r ($ aa );
  23. ?>

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.