PHP Learning notes to remove duplicate data from an array

Source: Internet
Author: User
Here's a summary of two ways to remove duplicate data from an array in PHP

1. Direct use of the Array_unique function

The Array_unique function allows you to remove duplicate values directly from an array, preserving only the first occurrence of values and their corresponding key values in a repeating value.

Specific instructions can be seen in the PHP manual

Example 1:

$input = Array ("A" = "green", "Red", "b" = "green", "Blue", "Red"), $result = Array_unique ($input);p Rint_r ($result) ;

Output at this time:

Array (    [A] = green    [0] = + red    [1] = blue)

Note: In the Array_unique function, if an int (3) and a string (1) ' 3 ' are present, the function will recognize both values as the same value, and will only retain the first occurrence of that value Example 2:

$input = Array (4, "4", "3", 4, 3, "3"), $result = Array_unique ($input); Var_dump ($result);

This will output:

Array (2) {  [0] = Int (4)  [2] = string (1) "3"}

2. Work with the Array_flip function and the Array_keys function

It is very convenient to take advantage of the Array_unique function, but there is a point where the Array_unique function does not change the value of the original key that is preserved, which is difficult for the operation of the new array in some cases, and can take advantage of the Array_flip function and Array_ Keys function

Array_flip function

The value of the specified array is swapped with the key value, the key name becomes the value, and the value in the original array is the key name, and if the same value occurs more than once, the last key will be the value, and all the others are lost. For example:

$trans = Array ("a" = = 1, "b" = 1, "c" = 2); $trans = Array_flip ($trans);p rint_r ($trans);

Output at this time:

Array (    [1] = b    [2] = c)

Array_keys function

The Array_keys function, which can return some or all of the key names in an array, is a good understanding, if Serch_value is specified, returns the key value of the specified query value, for example:

$array = Array (0 = +, "color" = "red");p Rint_r (Array_keys ($array)); $array = Array ("Blue", "Red", "green", "blue") , "Blue");p Rint_r (Array_keys ($array, "Blue")), $array = Array ("color" = = Array ("Blue", "Red", "green"),               "size"  = = Array ("Small", "medium", "large"));p Rint_r (Array_keys ($array));

This will output:

Array (    [0] = 0    [1] = + color) array (    [0] = 0    [1] = 3    [2] = 4) array (    [0] = col or    [1] = = size)

After the introduction of the use of two functions, when encountering a large number of data repeated array, want to remove the intermediate duplicate data and the key value from the ' 0 ' to start counting, you can first use the Array_flip function for the function, and then use the Array_keys function to the new array to achieve

Add: When the editor introduced the Array_keys function, it occurred to me that there should be a function to take out all the value of the array, check that there is a array_value function, this function can return all the values of an array and record into a new array, so you can also achieve the key value from ' 0 ' Start counting, so for the purposes of this article you can also use the Array_unique function first, and then use the Array_value function for it.

  • 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.