Remove duplicates from php array

WBOY
Release: 2023-05-05 21:42:09
Original
839 people have browsed it

In web development, arrays are an integral part. However, when processing large amounts of data, the array may contain a large number of duplicates, which can make data processing complex and inefficient. So, this article will explain how to remove duplicates from an array in PHP.

Method 1: Use the array_unique() function

The array_unique() function is a built-in function provided by PHP, which is used to remove duplicates from an array. It returns a new array containing only the unique values from the original array.

Using this function is very simple. You only need to pass the array that needs to be deduplicated as a parameter:

$array = array(1, 2, 2, 3, 4, 4, 5); $new_array = array_unique($array); print_r($new_array);
Copy after login

The above code will output:

Array ( [0] => 1 [1] => 2 [3] => 3 [4] => 4 [6] => 5 )
Copy after login
Copy after login

Method 2: Use Loops and the in_array() function

Another common method is to use a loop and the in_array() function to remove duplicates. This method works with various types of arrays, including associative arrays and multidimensional arrays.

The code to implement this method is as follows:

$array = array(1, 2, 2, 3, 4, 4, 5); $new_array = array(); foreach($array as $value){ if(!in_array($value, $new_array)){ $new_array[] = $value; } } print_r($new_array);
Copy after login

The above code will output the same result as the first method:

Array ( [0] => 1 [1] => 2 [3] => 3 [4] => 4 [6] => 5 )
Copy after login
Copy after login

Method three: use array_flip() and array_values( ) Function

If the values contained in the array are all integers or strings, you can also use the array_flip() and array_values() functions to remove duplicates.

The specific implementation method is as follows:

$array = array(1, 2, 2, 3, 4, 4, 5); $new_array = array_values(array_flip($array)); print_r($new_array);
Copy after login

The above code will output the same result as the first two methods:

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
Copy after login

In the above code, the array_flip() function will The values in are used as keys, and in the case of the same key name, subsequent values will overwrite previous values. Since we only need to remove duplicates, only one key name is retained in the final array, and other values corresponding to the same key name are deleted.

Finally, the array_values() function will rebuild the indexes of the array so that they are numbered consecutively starting from 0.

Summary

This article introduces several ways to remove duplicates from an array in PHP, including using the array_unique() function, loops and in_array() functions, as well as array_flip() and array_values () function. These methods are very simple and easy to understand, and can make array processing more efficient and convenient.

The above is the detailed content of Remove duplicates from php array. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!