How to remove duplicate values from an array in php
During development, we often encounter situations where we need to remove duplicate values from an array. As a popular server-side scripting language, PHP provides a wealth of functions and methods for operating arrays. This article will introduce several commonly used methods to remove duplicate values from arrays in PHP.
Method 1: array_unique()
array_unique()
is one of the most commonly used methods in PHP to remove duplicate values from an array. This function returns a new array containing only the unique values in the input array. The following is an example of use:
$array = array("apple", "banana", "cherry", "banana", "orange", "apple"); $newArray = array_unique($array); print_r($newArray);
The output result of the above code is:
Array ( [0] => apple [1] => banana [2] => cherry [4] => orange )
As can be seen from the output result, the array_unique()
function removes duplicates in the original array value and returns a new array. It should be noted that when this function removes duplicates, it determines whether there are duplicates based on the value, not the key.
Method 2: foreach loop to remove duplicates
In addition to using the array_unique()
function, we can also loop through the array and remove duplicate values. The following is an example of using foreach
loop to remove duplicates:
$array = array("apple", "banana", "cherry", "banana", "orange", "apple"); $newArray = array(); foreach ($array as $value) { if (!in_array($value, $newArray)) { $newArray[] = $value; } } print_r($newArray);
The output result of the above code is the same as method one. It should be noted that when using the in_array
function to determine whether a value is repeated, you need to use the !
negation sign to negate it to ensure that non-repeating values are added to the new in the array.
Method 3: array_flip()
array_flip()
The function can exchange the keys and values in the array to achieve the effect of deduplication. Before deduplication, we can use array_flip()
to exchange keys and values first, and then restore the array through array_flip()
. The following is an example:
$array = array("apple", "banana", "cherry", "banana", "orange", "apple"); $newArray = array_flip(array_flip($array)); print_r($newArray);
The output result of the above code is the same as method one and method two. It should be noted that if there are duplicate values in the original array, the subsequent values will be lost after exchanging the key-value pairs, leaving only the last one. Therefore, when using array_flip()
, you need to first use array_flip()
to remove duplicate key values, and then use array_flip()
to restore the original array. .
In short, the above are three methods commonly used in PHP to remove duplicate values from arrays. Depending on actual needs, choosing different methods can improve program efficiency and make the code more concise and easy to understand.
The above is the detailed content of How to remove duplicate values from an array in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics









