Home >Backend Development >PHP Problem >How to remove duplicate strings from php array

How to remove duplicate strings from php array

PHPz
PHPzOriginal
2023-03-24 09:44:531432browse

In PHP programming, arrays are often used as a tool for storing and processing data. However, this can cause some problems when the array contains repeated strings. Fortunately, PHP provides some built-in functions and tricks to remove duplicate strings from arrays.

In this article, we will learn how to write a function to remove duplicate strings from an array using PHP.

  1. Use array_unique function

PHP built-in function array_unique can very easily remove duplicate strings in an array. This function returns a new array with all duplicate values ​​removed. The specific usage is as follows:

$originalArray = array("apple","orange","banana","orange","kiwi","banana");
$uniqueArray = array_unique($originalArray);
print_r($uniqueArray);

The above code will return the following results:

Array
(
    [0] => apple
    [1] => orange
    [2] => banana
    [4] => kiwi
)

By using the array_unique function, we can easily remove duplicate strings in the original array.

  1. Use foreach loop to achieve

We can also use foreach loop to remove duplicates. The following is the PHP code that uses a foreach loop to remove duplicates:

$originalArray = array("apple","orange","banana","orange","kiwi","banana");
$newArray = array();
foreach ($originalArray as $value) {
   if (!in_array($value, $newArray)) {
       $newArray []= $value;
   }
}
print_r($newArray);

Use a foreach loop to traverse the array and check whether the current value already exists in the new array. If it doesn't exist, add it to the new array. If present, the value is ignored. This method can also effectively remove duplicate strings in the array and return the following results:

Array
(
    [0] => apple
    [1] => orange
    [2] => banana
    [4] => kiwi
)
  1. Use the array_flip and array_keys functions

Another One way to deduplicate an array is to use the PHP built-in functions array_flip and array_keys. The array_flip function can exchange the keys and values ​​of an array, and the array_keys function can return all the key names in an array. The following is the PHP code that uses array_flip and array_keys to implement deduplication:

$originalArray = array("apple","orange","banana","orange","kiwi","banana");
$newArray = array_flip(array_keys(array_flip($originalArray)));
print_r($newArray);

In this example, the array_flip function first swaps the key names and key values ​​​​of the original array, removes duplicate values, and then uses the array_keys function to return Replace all key names. Finally, we use array_flip to swap key names and key values ​​again. In this way, we get a new array after deduplication, and the result is the same as the first two methods:

Array
(
    [0] => apple
    [1] => orange
    [2] => banana
    [4] => kiwi
)

Summary

In PHP programming, we often Process arrays. This can cause some problems if there are duplicate strings in the array. Fortunately, PHP provides some built-in functions and tricks to remove duplicate strings from arrays. In this article, we learned three ways to achieve this function, using the array_unique function, using the foreach loop and the array_flip and array_keys functions. Either way, they can effectively remove duplicate strings in an array.

The above is the detailed content of How to remove duplicate strings from 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