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

PHPz
PHPzOriginal
2023-04-25 17:36:23836browse

In web development, array operations are a very common and essential skill. There are many ways to operate on arrays in PHP, including adding, modifying, and deleting elements from an array. This article will focus on how to delete identical elements using one of the PHP array operations.

The same elements in the array usually have a negative impact on the code logic. For example, duplicate data will make the code execution inefficient. Fortunately, PHP provides a solution to this problem.

First, let’s look at several ways to delete identical elements in an array in PHP.

Method 1: Use the array_unique function

The array_unique function is a packaging function in PHP used to remove duplicate values ​​from an array. This function returns a new array containing only the unique values ​​from the original array. The specific usage is as follows:

$array = array('东京', '纽约', '上海', '纽约');
$array = array_unique($array);
print_r($array);

Output result:

Array
(
    [0] => 东京
    [1] => 纽约
    [2] => 上海
)

Method 2: Use for loop

Use for loop to traverse the array and compare whether each element is the same as the previous element , if they are the same, delete the element. The specific code is as follows:

$array = array('东京', '纽约', '上海', '纽约');
for ($i = 0; $i < count($array); $i++) {
    for ($j = $i + 1; $j < count($array); $j++) {
        if ($array[$i] == $array[$j]) {
            array_splice($array, $j, 1);
            $j--;
        }
    }
}
print_r($array);

Output result:

Array
(
    [0] => 东京
    [1] => 纽约
    [2] => 上海
)

Method 3: Use array_flip and array_keys functions

array_flip function can exchange keys and values ​​​​in the array, and array_keys function All keys in the array can be returned. Combining these two functions, we can easily remove duplicate elements from the array.

The specific code is as follows:

$array = array('东京', '纽约', '上海', '纽约');
$array = array_flip($array);
$array = array_keys($array);
print_r($array);

Output result:

Array
(
    [0] => 东京
    [1] => 纽约
    [2] => 上海
)

The above three methods can all achieve the operation of deleting the same elements in the array, but the implementation methods between them But each one is different. Which method is suitable for your development scenario depends on the situation.

It is worth mentioning that the operation of deleting the same elements in the array does not necessarily require the use of functions provided by PHP. Sometimes we can also implement this operation by writing our own code. The following is an example:

Method 4: Write your own deletion function

function delSameValue(array $array) {
    $result = array();
    foreach ($array as $value) {
        if (!in_array($value, $result)) {
            $result[] = $value;
        }
    }
    return $result;
}

$array = array('东京', '纽约', '上海', '纽约');
$array = delSameValue($array);
print_r($array);

Output result:

Array
(
    [0] => 东京
    [1] => 纽约
    [2] => 上海
)

The above methods are just a few ways to delete the same elements in the array. If You need further array operations, and you need to continue learning other related functions and techniques in PHP. At the same time, you need to pay attention to choosing the optimal method to solve the problem according to the actual situation when processing arrays, so as to obtain better execution efficiency and simpler code.

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