PHP Array de-weight method reference (one-dimensional array de-heavy, two-dimensional array de-weight)

Source: Internet
Author: User
This article introduces a method for removing duplicates of PHP arrays, including the deduplication of one-dimensional arrays and the de-weight of two-dimensional arrays. Refer to a friend who needs it.

First, take a look at the removal of one-dimensional array duplicates.

Use the Array_unique function, for example:

 
      

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

Then we look at the method of removing two-dimensional array duplicates.

For a two-dimensional array, it is considered that the value of one key name cannot be duplicated, and duplicates are deleted because the inner one-dimensional array cannot be identical.

This is illustrated by specific examples.

1, because the value of a key name cannot be duplicated, delete duplicates.

 
      $v)   {      if (In_array ($v [$key], $tmp _arr))//Search for $v[$key] is present in the $tmp_arr array, if present returns True     {        unset ($arr [$k]);     }   else {       $tmp _arr[] = $v [$key];     }   } sort ($arr),//sort function array to return $arr;} $AA = Array (array (' ID ' =&G T 123, ' name ' = ' Zhang San '), array (' id ' = = 123, ' name ' = ' John Doe '), array (' id ' = = 124, ' name ' = = ' Harry '), Array (' ID ' =& Gt The ' name ' = ' Zhao Liu '), array (' id ' = = 126, ' name ' = ' Zhao Liu ')); $key = ' id '; Assoc_unique (& $aa, $key); Print_r ($AA); by bbs.it-home.org?>

Output: Array ([0] = = Array ([id] = = 123 [Name] = + Zhang San) [1] = = Array ([id] = = 124 [name] = Harry) [2] =&G T Array ([id] = [name] = + Zhao Liu) [3] = = Array ([id] = 126 [Name] = Zhao Liu))

2, because an internal one-dimensional array cannot be exactly the same, and delete duplicates

 
      $v) {         $temp [$k] = Explode (",", $v);   Re-assemble the disassembled array     }     return $temp;} $aa = array (' id ' = ' = ' 123, ' name ' = ' Zhang San '), array (' id ' = = 123, ' name ' = = ' John Doe '), array (' id ' = = 124, ' name ' = = ' Harry '), Array (' id ' = = 123, ' name ' = = ' John Doe '), array (' id ' = = 126, ' Name ' = ' Zhao Liu ')); $BB =ARRAY_UNIQUE_FB ($AA); Print_r ($BB); by bbs.it-home.org?>

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.