Home > Backend Development > PHP Problem > How to remove unique values ​​from php array

How to remove unique values ​​from php array

PHPz
Release: 2023-04-25 14:10:57
Original
553 people have browsed it

PHP is a very popular dynamic programming language that is widely used in web development. During the development process, we often need to process data, and arrays are a very common data structure. When we need to remove unique values ​​from an array, we can use the following method:

Method 1: Use the array_unique function to remove duplicates

The array_unique function is one of the commonly used functions in PHP. Can remove duplicate values ​​from an array. This function returns an array with no duplicate values. The usage method is as follows:

<?php
    $arr = array(1, 1, 2, 2, 3, 4, 4, 5);
    $unique_arr = array_unique($arr);
    print_r($unique_arr);
?>
Copy after login

The output result is: Array ( [0] => 1 [2] => 2 [4] => 3 [5] => 4 [6] => ; 5)

Method 2: Use foreach loop to remove duplicates

When we don’t want to use the array_unique function, we can also use foreach loop to remove duplicates. The specific steps are as follows:

  1. Create a new empty array $result to store the results after deduplication.
  2. Use foreach loop to traverse the original array $arr.
  3. Determine whether each value in the original array exists in the new array $result. If it does not exist, add it to $result.

Code example:

<?php
    $arr = array(1, 1, 2, 2, 3, 4, 4, 5);
    $result = array();
    foreach($arr as $a){
        if(!in_array($a, $result)){
            $result[] = $a;
        }
    }
    print_r($result);
?>
Copy after login

The output result is: Array ( [0] => 1 [1] => 2 [2] => 3 [3] = > 4 [4] => 5 )

Method 3: Use the array_flip function to remove duplicates

The array_flip function can exchange the keys and values ​​​​in the array and use it as the value of the new array keys to remove duplicates. The usage method is as follows:

<?php
    $arr = array(1, 1, 2, 2, 3, 4, 4, 5);
    $unique_arr = array_flip(array_flip($arr));
    print_r($unique_arr);
?>
Copy after login

The output result is: Array ( [0] => 1 [2] => 2 [4] => 3 [5] => 4 [6] => ; 5)

Summary:

The above are several ways to remove unique values ​​from an array in PHP. Either way, the same functionality can be achieved. In actual development, choosing the appropriate method based on requirements and performance is the most important.

The above is the detailed content of How to remove unique values ​​from php array. 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