PHP learning notes remove repeated data from the array

Source: Internet
Author: User
PHP learning notes remove repeated data in the array. here are two methods to remove repeated data in the array in PHP:

1. directly use the array_unique function

The array_unique function can be used to directly remove duplicate values in an array. only the values that appear for the first time in the repeated values and their corresponding key values are retained.

For more information, see The PHP Manual.

Example 1:

$input = array("a" => "green", "red", "b" => "green", "blue", "red");$result = array_unique($input);print_r($result);

Output:

Array(    [a] => green    [0] => red    [1] => blue)

Note: In the array_unique function, if int (3) and string (1) '3' appear, the function recognizes the two values as the same value, only the values that appear first are retained. Example 2:

$input = array(4, "4", "3", 4, 3, "3");$result = array_unique($input);var_dump($result);

The output is as follows:

array(2) {  [0] => int(4)  [2] => string(1) "3"}
2. use the array_flip function and the array_keys function together.

It is very convenient to use the array_unique function. However, the array_unique function does not change the reserved key value, which makes operations on new arrays difficult in some situations, now we can use the array_flip function and the array_keys function.

Array_flip function

You can call the key-value pairs of the specified array to convert the key name into a value. The value in the original array is the key name. if the same value appears multiple times, the last key name is used as its value, and all others are lost. For example:

$trans = array("a" => 1, "b" => 1, "c" => 2);$trans = array_flip($trans);print_r($trans);

Output:

Array(    [1] => b    [2] => c)
Array_keys function

The array_keys function can return some or all key names in the array. this is easy to understand. if serch_value is specified, the key value of the specified query value is returned, for example:

$array = array(0 => 100, "color" => "red");print_r(array_keys($array));$array = array("blue", "red", "green", "blue", "blue");print_r(array_keys($array, "blue"));$array = array("color" => array("blue", "red", "green"),               "size"  => array("small", "medium", "large"));print_r(array_keys($array));

The output is as follows:

Array(    [0] => 0    [1] => color)Array(    [0] => 0    [1] => 3    [2] => 4)Array(    [0] => color    [1] => size)

After introducing the use of the two functions, when you encounter an array with a large number of data duplicates, you want to remove the duplicated data and want to count the key value from '0, you can use the array_flip function for the function, and then use the array_keys function for the new array.

Supplement: when editing and introducing the array_keys function, I suddenly thought that there should be a function that can retrieve the values of all arrays. I checked the array_value function, this function can return all the values of an array and record the values in the new array. This function can also count the key value from '0, therefore, you can use the array_unique function and then use the array_value function for the purpose of this article.

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.