Simple example of php array de-weight (one-dimensional, two-dimensional array de-weight)

Source: Internet
Author: User
Tags pear
    1. $aa =array ("Apple", "banana", "pear", "apple", "wail", "Watermalon");
    2. $BB =array_unique ($AA);
    3. Print_r ($BB);
    4. ?>
Copy Code

Output: Array ([0] = Apple [1] = banana [2] = pear [4] = wail [5] = Watermalon).

Two, php two-dimensional array of duplicates: For a two-dimensional array we can be discussed, one is because the value of a key name cannot be duplicated, delete duplicates, and the other because the inner one-dimensional array can not be identical, and delete duplicates.

Example 1, because the value of a key name cannot be duplicated, delete duplicates. 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 $v[$key] is present in the $tmp_arr array, if there is a return true
    7. {//bbs.it-home.org
    8. Unset ($arr [$k]);
    9. }
    10. else {
    11. $tmp _arr[] = $v [$key];
    12. }
    13. }
    14. Sort ($arr); Sort function sorting an array
    15. return $arr;
    16. }
    17. $AA = Array (
    18. Array (' id ' = = 123, ' name ' = ' Zhang San '),
    19. Array (' id ' = = 123, ' name ' = ' John Doe '),
    20. Array (' id ' = = 124, ' name ' = ' Harry '),
    21. Array (' id ' = ' = ', ' name ' = ' Zhao Liu '),
    22. Array (' id ' = = 126, ' name ' = ' Zhao Liu ')
    23. );
    24. $key = ' id ';
    25. Assoc_unique (& $aa, $key);
    26. Print_r ($AA);
    27. ?>
Copy Code

Output: Array ([0] = = Array ([id] = = 123 [Name] = + Zhang San) [1] = = Array ([id] = = 124 [name] = Harry) [2] = = Array ([id] = [name] = + Zhao Liu) [3] = = Array ([id] = 126 [name] + Zhao Liu)) Example 2, since the inner one-dimensional array cannot be identical, the deletion of the heavy The complex. Code:

    1. function ARRAY_UNIQUE_FB ($array 2D) {
    2. foreach ($array 2D as $v) {
    3. $v = Join (",", $v); Dimension, you can also convert one-dimensional array to a comma-concatenated string using implode
    4. $temp [] = $v;
    5. }//Bbs.it-home.org
    6. $temp = Array_unique ($temp); Remove duplicate strings, that is, duplicate one-dimensional arrays
    7. foreach ($temp as $k = = $v) {
    8. $temp [$k] = Explode (",", $v); Re-assemble the disassembled array
    9. }
    10. return $temp;
    11. }
    12. $AA = Array (
    13. Array (' id ' = = 123, ' name ' = ' Zhang San '),
    14. Array (' id ' = = 123, ' name ' = ' John Doe '),
    15. Array (' id ' = = 124, ' name ' = ' Harry '),
    16. Array (' id ' = = 123, ' name ' = ' John Doe '),
    17. Array (' id ' = = 126, ' name ' = ' Zhao Liu ')
    18. );
    19. $BB =ARRAY_UNIQUE_FB ($AA);
    20. Print_r ($BB)
    21. ?>
Copy Code

Output Result:

Array ([0] = = Array ([0] = = 123 [1] = Zhang San) [1] = = Array ([0] = = 123 [1] = = Li four) [2] + = Array ([0 ] = 124 [1] = Harry) [4] = = Array ([0] = = 126 [1] = Zhao Liu))
  • 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.