Home  >  Article  >  Backend Development  >  How to delete the same elements in an array in php

How to delete the same elements in an array in php

zbt
zbtOriginal
2023-08-24 15:53:041325browse

php can use the array_unique() function, loop traversal, array_flip() and array_values() functions to delete the same elements of the array. Detailed introduction: 1. array_unique() function, which accepts an array as a parameter and returns a new array containing only unique elements; 2. Using loop traversal, first create an empty array $uniqueArray, and then use foreach Loop through the original array and so on.

How to delete the same elements in an array in php

The operating environment of this tutorial: windows10 system, php8.1.3 version, DELL G3 computer.

PHP is a widely used scripting language for developing web applications. In PHP, array is a very common and useful data structure. Sometimes, we may need to remove identical elements from an array to get a unique set of elements. This article will introduce several methods to achieve this goal.

Method 1: Use array_unique() function

PHP provides a built-in function array_unique() for removing duplicate elements from an array. This function accepts an array as argument and returns a new array containing only unique elements.

The sample code is as follows:

$originalArray = array(1, 2, 3, 4, 2, 3, 5);
$uniqueArray = array_unique($originalArray);

In the above example, $originalArray is the original array, containing some repeated elements. By calling the array_unique() function, we get a new array $uniqueArray, which contains only unique elements.

Method 2: Use loop traversal

In addition to using the built-in function array_unique(), we can also use loop traversal to remove duplicate elements in the array. This method works with all versions of PHP.

The sample code is as follows:

$originalArray = array(1, 2, 3, 4, 2, 3, 5);
$uniqueArray = array();
foreach ($originalArray as $element) {
if (!in_array($element, $uniqueArray)) {
$uniqueArray[] = $element;
}
}

In the above example, we first create an empty array $uniqueArray, and then use a foreach loop to traverse each element in the original array $originalArray. In each loop, we use the in_array() function to check if the current element already exists in $uniqueArray. If it doesn't exist, we add the element to $uniqueArray.

The efficiency of this method may be affected by the size of the array and the number of repeated elements. If the original array is large, or contains a large number of repeated elements, it may be more efficient to use the array_unique() function.

Method 3: Use the array_flip() and array_values() functions

Another way to remove duplicate elements from an array is to use the array_flip() and array_values() functions The combination. This method swaps the keys and values ​​of the array and then re-indexes the array using the array_values() function.

The sample code is as follows:

$originalArray = array(1, 2, 3, 4, 2, 3, 5);
$flippedArray = array_flip($originalArray);
$uniqueArray = array_values($flippedArray);

In the above example, we first use the array_flip() function to exchange the keys and values ​​​​of the original array $originalArray to obtain a new array $flippedArray . We then use the array_values() function to re-index $flippedArray, resulting in a new array $uniqueArray containing only unique elements.

Summary:

This article introduces three methods to remove duplicate elements in PHP arrays. Using the array_unique() function is the simplest and most efficient method and works on newer versions of PHP. If your PHP version is older or has higher performance requirements, you can consider using loop traversal or a combination of array_flip() and array_values() functions. No matter which method you choose, you can easily remove duplicate elements from an array and get a unique set of elements. .

The above is the detailed content of How to delete the same elements in an array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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