Home > Backend Development > PHP Problem > How to remove identical arrays in php

How to remove identical arrays in php

PHPz
Release: 2023-04-19 09:41:43
Original
457 people have browsed it

In PHP, sometimes we need to deduplicate an array, that is, remove repeated elements. This process may seem simple, but different situations may require different approaches. Next, we will discuss some common array deduplication methods and their application scenarios.

1. Use the PHP built-in function array_unique()

PHP provides a built-in function array_unique(), which can directly perform deduplication operations on an array. The syntax for using this function is as follows:

array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )
Copy after login

Among them, $array is the array to be duplicated, $sort_flags is an optional parameter, used to specify the comparison method of elements, and can take the following values:

  • SORT_REGULAR - Compare elements in the usual way (without changing the data type of the elements)
  • SORT_NUMERIC - Compare elements based on numeric size
  • SORT_STRING - Compare elements based on strings
  • SORT_LOCALE_STRING - Compares elements according to the current locale
  • SORT_NATURAL - Compares elements according to their natural ordering

The sample code is as follows:

$arr = array(1, 2, 2, 3, 4, 4, 5);
$new_arr = array_unique($arr);
print_r($new_arr); // 输出:Array ( [0] => 1 [1] => 2 [3] => 3 [4] => 4 [6] => 5 )
Copy after login

Advantages of using array_unique() The reason is that it is simple and easy to use, and the array deduplication operation can be completed in one line of code. The disadvantage is that it can only handle one-dimensional arrays. If you want to handle multi-dimensional arrays, additional processing is required.

2. Use loop conditional judgment

If you want to process a multi-dimensional array, you can use loop conditional judgment to remove duplicates. The idea of ​​this method is to traverse the array, compare each element in turn with the previous element in the array (except those that have been deduplicated), and skip if duplicates are found, otherwise add the element to the deduplicated array. The sample code is as follows:

$arr = array(
    array(1, 2, 2),
    array(3, 4, 4),
    array(5, 6, 7),
    array(1, 2, 2)
);

$new_arr = array();
foreach ($arr as $value) {
    if (!in_array($value, $new_arr)) {
        $new_arr[] = $value;
    }
}
print_r($new_arr); // 输出:Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 2 ) [1] => Array ( [0] => 3 [1] => 4 [2] => 4 ) [2] => Array ( [0] => 5 [1] => 6 [2] => 7 ) )
Copy after login

Using loops The advantage of conditional judgment is that it is suitable for different forms of arrays, including multi-dimensional arrays, and can maintain good performance even when the amount of data is large. The disadvantage is that it requires writing more code, and it is easy to make mistakes if there are slight flaws.

3. Use PHP built-in function array_reduce()

In addition to array_unique(), PHP also provides a built-in function array_reduce(), which can be used to perform reduction operations on arrays. For array deduplication, we can convert the array into an associative array with array elements as keys and empty values, and then merge it with the original array. If the same elements are encountered, skip them, otherwise add them to the merge in the subsequent array. The sample code is as follows:

$arr = array(1, 2, 2, 3, 4, 4, 5);

$new_arr = array_reduce($arr, function($carry, $item) {
    if(!array_key_exists($item, $carry)) {
        $carry[$item] = null;
    }
    return $carry;
}, array());

print_r(array_keys($new_arr)); // 输出:Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
Copy after login

The advantage of using array_reduce() is that it can flexibly handle different forms of arrays and the code is concise. The disadvantage is that the performance may be reduced for arrays with large amounts of data.

To sum up, there are many ways to deduplicate arrays in PHP, and you can choose the appropriate method according to the specific situation. For one-dimensional arrays, it is recommended to use array_unique() first; for multi-dimensional arrays, it is recommended to use loop conditional judgment or array_reduce(). Program performance and data quality can be improved by deduplicating data.

The above is the detailed content of How to remove identical arrays in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template