Simple example of php array deduplication (one-dimensional and two-dimensional array deduplication)-php Tutorial

Source: Internet
Author: User
Simple example of php array deduplication (one-dimensional and two-dimensional array deduplication)

  1. $ Aa = array ("apple", "banana", "pear", "apple", "wail", "watermalon ");
  2. $ Bb = array_unique ($ aa );
  3. Print_r ($ bb );
  4. ?>

Output result: Array ([0] => apple [1] => banana [2] => pear [4] => wail [5] => watermalon ).

2. duplicate items of two-dimensional PHP arrays: We will discuss two types of two-dimensional arrays. one is because the values of one-click names cannot be repeated and repeated items are deleted; in addition, duplicate items are deleted because the internal one-dimensional arrays cannot be identical.

Example 1: delete duplicate items because the values of one-click names cannot be repeated. Code:

  1. Function assoc_unique ($ arr, $ key)
  2. {
  3. $ Tmp_arr = array ();
  4. Foreach ($ arr as $ k => $ v)
  5. {
  6. If (in_array ($ v [$ key], $ tmp_arr) // search for $ v [$ key] in the $ tmp_arr array. if yes, true is returned.
  7. // Bbs.it-home.org
  8. Unset ($ arr [$ k]);
  9. }
  10. Else {
  11. $ Tmp_arr [] = $ v [$ key];
  12. }
  13. }
  14. Sort ($ arr); // sort the array
  15. Return $ arr;
  16. }
  17. $ Aa = array (
  18. Array ('id' => 123, 'name' => 'Zhang San '),
  19. Array ('id' => 123, 'name' => 'Lily '),
  20. Array ('id' => 124, 'name' => 'Wang Wu '),
  21. Array ('id' => 125, 'name' => 'Zhao Liu '),
  22. Array ('id' => 126, 'name' => 'Zhao Liu ')
  23. );
  24. $ Key = 'id ';
  25. Assoc_unique (& $ aa, $ key );
  26. Print_r ($ aa );
  27. ?>

Output result: Array ([0] => Array ([id] => 123 [name] => Zhang San) [1] => Array ([id] => 124 [name] => Wang Wu) [2] => Array ([id] => 125 [name] => Zhao 6) [3] => Array ([id] => 126 [name] => Zhao 6) Example 2: duplicate items are deleted because the internal one-dimensional arrays cannot be identical. Code:

  1. Function array_unique_fb ($ array2D ){
  2. Foreach ($ array2D as $ v ){
  3. $ V = join (",", $ v); // dimension reduction. you can also use implode to convert a one-dimensional array to a string connected with commas (,).
  4. $ Temp [] = $ v;
  5. } // Bbs.it-home.org
  6. $ Temp = array_unique ($ temp); // remove the duplicate string, that is, the duplicate one-dimensional array.
  7. Foreach ($ temp as $ k => $ v ){
  8. $ Temp [$ k] = explode (",", $ v); // re-assemble the split array
  9. }
  10. Return $ temp;
  11. }
  12. $ Aa = array (
  13. Array ('id' => 123, 'name' => 'Zhang San '),
  14. Array ('id' => 123, 'name' => 'Lily '),
  15. Array ('id' => 124, 'name' => 'Wang Wu '),
  16. Array ('id' => 123, 'name' => 'Lily '),
  17. Array ('id' => 126, 'name' => 'Zhao Liu ')
  18. );
  19. $ Bb = array_unique_fb ($ aa );
  20. Print_r ($ bb)
  21. ?>

Output result:

Array ([0] => Array ([0] => 123 [1] => Zhang San) [1] => Array ([0] => 123 [1] => Li Si) [2] => Array ([0] => 124 [1] => Wang Wu) [4] => Array ([0] => 126 [1] => Zhao 6 ))

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.