Home  >  Article  >  Backend Development  >  How to flip an array in php to remove duplicates

How to flip an array in php to remove duplicates

PHPz
PHPzOriginal
2023-04-25 09:05:04361browse

In PHP development, arrays are a very commonly used data structure. In the actual development process, we may encounter the need to flip and deduplicate arrays. So this article will introduce the implementation methods for these two problems respectively.

1. Array flipping

Flipping an array means reversing the order of the elements in the array, that is, changing the last element in the array to the first element, and the second to last element to the first element. two elements, and so on. For example, flip the array [1, 2, 3] to [3, 2, 1].

The following is a method to implement flipping an array:

function reverseArray($array) {
    $result = array();
    for ($i = count($array) - 1; $i >= 0; $i--) {
        $result[] = $array[$i];
    }
    return $result;
}

// 示例
$arr = array(1, 2, 3);
$res = reverseArray($arr);
print_r($res);  // 输出结果为 [3, 2, 1]

In the above code, first define a $result array to save the flipped result, and then use a for loop to retrieve the last element of the original array Start traversing, insert each element into the $result array in turn, and finally return the $result array.

2. Array deduplication

Array deduplication refers to retaining only one repeated element in the array and deleting the remaining elements. For example, after deduplicating the array [1, 2, 2, 3, 3, 3], the result is [1, 2, 3].

The following are some methods to achieve array deduplication:

Method 1: Use array_unique() function

PHP provides a built-in function array_unique(), which can Quickly implement array deduplication function. The following is a sample code:

$arr = array(1, 2, 2, 3, 3, 3);
$res = array_unique($arr);
print_r($res);  // 输出结果为 [1, 2, 3]

In the above code, you only need to call the array_unique() function to implement the array deduplication operation.

Method 2: Use loop traversal to remove duplicates

The method of looping through the array and comparing the array elements one by one is relatively simple and direct. The specific implementation code is as follows:

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

// 示例
$arr = array(1, 2, 2, 3, 3, 3);
$res = uniqueArray($arr);
print_r($res);  // 输出结果为 [1, 2, 3]

In the above code, use a foreach loop to traverse each element in the original array, and use the in_array() function to determine whether the element already exists in the result array $result. If it does not exist, it will It is inserted into the $result array.

Method 3: Use array_flip() function

array_flip() function to swap keys and values ​​in an array. Using this function, we can easily remove duplicate elements from the array. The specific implementation code is as follows:

function uniqueArray($array) {
    $result = array_flip($array);
    return array_keys($result);
}

// 示例
$arr = array(1, 2, 2, 3, 3, 3);
$res = uniqueArray($arr);
print_r($res);  // 输出结果为 [1, 2, 3]

In the above code, first use the array_flip() function to exchange the keys and values ​​in the original array, and then use the array_keys() function to obtain the result that is the deduplicated array.

Summary

This article introduces the implementation methods of array flipping and array deduplication in PHP development. Array flipping is implemented by inserting array elements into a new array through a loop, while array deduplication is achieved. This can be achieved by calling the built-in function array_unique(), or by looping through the array_flip() function. Just choose the corresponding implementation method according to the actual development needs.

The above is the detailed content of How to flip an array in php to remove duplicates. 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