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

How to delete the same elements in php array

PHPz
PHPzOriginal
2023-04-25 09:06:13334browse

In PHP, if the same elements exist in the array, we may need to delete them. This question seems simple, but it actually involves some technical details. Let's discuss how to delete the same elements in a PHP array.

Method 1: Use array_unique function

PHP provides a function array_unique(), which can remove duplicate elements from an array and keep only one of them. This function is very simple, we only need to pass the array that needs to be deduplicated as a parameter to it.

Sample code:

$array = array('a', 'b', 'c', 'a', 'b');
$unique_array = array_unique($array);
print_r($unique_array);

Output result:

Array
(
    [0] => a
    [1] => b
    [2] => c
)

As can be seen from the above code, the array_unique() function only retains the only element in the original array. But it does not actually delete the duplicate elements in the array, but retains all the duplicate elements. If we want to really remove duplicate elements, we need to use it in conjunction with the unset() function.

Method 2: Using loops and unset functions

We can use a loop to traverse each element in the array, and determine whether there are duplicate elements through comparison, and delete them if they exist.

Sample code:

$array = array('a', 'b', 'c', 'a', 'b');
for($i = 0; $i < count($array); $i++){
    for($j = $i + 1; $j < count($array); $j++){
        if($array[$i] == $array[$j]){
            unset($array[$j]);
        }
    }
}
print_r($array);

Output result:

Array
(
    [0] => a
    [1] => b
    [2] => c
)

As can be seen from the above code, we traverse all the elements in the array through two for loops, and then judge Whether there are duplicate elements and delete them through the unset() function. The final output result is the array after deduplication.

It should be noted that when using the unset() function, if we directly delete the elements in the array, the index of the array will change. In order to avoid problems caused by index changes, we can use the array_values() function to rearrange the indexes of the array.

$array = array('a', 'b', 'c', 'a', 'b');
for($i = 0; $i < count($array); $i++){
    for($j = $i + 1; $j < count($array); $j++){
        if($array[$i] == $array[$j]){
            unset($array[$j]);
        }
    }
}
$array = array_values($array);
print_r($array);

Output result:

Array
(
    [0] => a
    [1] => b
    [2] => c
)

Conclusion

The above are two methods of deleting duplicate elements in an array in PHP. From an efficiency point of view, using the array_unique() function is more efficient, but it should be noted that it returns a new array rather than directly deleting the original array; the method of using a loop combined with the unset() function is slightly less efficient, but it can actually delete Repeating elements also avoids changes in array indexes. Depending on the specific needs and actual scenarios, we can choose the appropriate method to handle repeated elements in PHP arrays.

The above is the detailed content of How to delete the same elements in php array. 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