A php function that removes repeated items in a two-dimensional array

Source: Internet
Author: User
A php function that removes repeated items in a two-dimensional array

  1. // Function 1. remove duplicate values from two-dimensional arrays

  2. Function array_unique_fb ($ array2D ){
  3. Foreach ($ array2D as $ v ){
  4. $ V = join (",", $ v); // dimension reduction. you can also use implode to convert a one-dimensional array to a string connected with commas (,).
  5. $ Temp [] = $ v;
  6. }
  7. $ Temp = array_unique ($ temp); // remove the duplicate string, that is, the duplicate one-dimensional array.
  8. Foreach ($ temp as $ k => $ v ){
  9. $ Temp [$ k] = explode (",", $ v); // re-assemble the split array
  10. }
  11. Return $ temp;
  12. }

  13. // Function 2. remove duplicate values from two-dimensional arrays and retain key values.

  14. Function array_unique_fb ($ array2D ){
  15. Foreach ($ array2D as $ k => $ v ){
  16. $ V = join (",", $ v); // dimension reduction. you can also use implode to convert a one-dimensional array to a string connected with commas (,).
  17. $ Temp [$ k] = $ v;
  18. }
  19. $ Temp = array_unique ($ temp); // remove the duplicate string, that is, the duplicate one-dimensional array.
  20. Foreach ($ temp as $ k => $ v ){
  21. $ Array = explode (",", $ v); // re-assemble the split array
  22. $ Temp2 [$ k] ["id"] = $ array [0];
  23. $ Temp2 [$ k] ["litpic"] = $ array [1];
  24. $ Temp2 [$ k] ["title"] = $ array [2];
  25. $ Temp2 [$ k] ["address"] = $ array [3];
  26. $ Temp2 [$ k] ["starttime"] = $ array [4];
  27. $ Temp2 [$ k] ["endtime"] = $ array [5];
  28. $ Temp2 [$ k] ["classid"] = $ array [6];
  29. $ Temp2 [$ k] ["ename"] = $ array [7];
  30. }
  31. Return $ temp2;
  32. }
  33. ?>

Call example:

  1. $ Arr = array ("a" => array ("a" => "welcome", "B" => "to", "c" => "bbs.it-home.org "),
  2. "B" => array ("a" => "Programmer's House", "B" => "jbxue.com", "c" => "beijing ")
  3. );
  4. $ Arr2 = array_unique_fb ($ arr)
  5. ?>

Articles you may be interested in: php two examples of removing duplicate data from arrays PHP two-dimensional array de-duplicated instance parsing php array de-duplicated function code example php array de-duplicated (one-dimensional, two-dimensional array de-duplicated) php array deduplication method reference (one-dimensional array deduplication, two-dimensional array deduplication) php method for judging and removing repeated data from arrays removing repeated combinations in two-dimensional arrays implementation code php two-dimensional array de-duplication user-defined function php array_unique one-dimensional array removal example php array de-duplication example of duplicate values

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.