Home  >  Article  >  Backend Development  >  How to remove duplicate values ​​from an array in php

How to remove duplicate values ​​from an array in php

PHPz
PHPzOriginal
2023-04-23 09:18:40939browse

With the continuous development of the Internet, the importance of Web development has attracted more and more attention. In the process of Web development, the PHP language, as one of the most basic languages, is widely used in server-side programming, and arrays are one of the important components of the PHP language.

Normally, when we process data, we sometimes need to use arrays, and we expect the internal values ​​​​to be non-repeating. If there are the same values, repeated results will easily occur. To do this, we need to use a special function to remove duplicate values ​​from a PHP array. Let’s take a closer look.

To remove duplicate values ​​from an array in PHP, the array_unique() function is actually used. Next, we will introduce how to use it with some examples.

  1. Remove duplicate values ​​in index array

To remove duplicate elements from an array in PHP, you can use PHP's built-in array_unique() function. It can help us filter duplicate elements in the array, retaining the first occurrence of the element in the file, and all subsequent copies will be deleted.

The following shows how to use the array_unique() function to remove duplicate values ​​in the index array:

<?php

$names = array("John", "Peter", "David", "Peter", "Mike", "David", "Mark", "Peter");

$unique_names = array_unique($names);

print_r($unique_names);

?>

The output result is:

Array
(
    [0] => John
    [1] => Peter
    [2] => David
    [4] => Mike
    [6] => Mark
)

We can see that the indexes are 3 and 5 The elements "Peter" and "David" have been removed, leaving only one. This is because the array_unique() function retains the first occurrence of an element in the array and deletes all subsequent occurrences of the element.

  1. Remove duplicate values ​​from associative arrays

In addition to indexing arrays, we can also use the array_unique() function to remove duplicate values ​​from associative arrays in PHP. For associative arrays, care needs to be taken when specifying keys.

The following shows how to use the array_unique() function to remove duplicate values ​​in an associative array:

<?php

$users = array(
    "id1" => array("name" => "John", "email" => "john@example.com"),
    "id2" => array("name" => "Peter", "email" => "peter@example.com"),
    "id3" => array("name" => "David", "email" => "david@example.com"),
    "id4" => array("name" => "Peter", "email" => "peter@example.com"),
    "id5" => array("name" => "Mike", "email" => "mike@example.com"),
    "id6" => array("name" => "David", "email" => "david@example.com"),
);

// 指定"name"键去除重复值
$unique_users = array_map("unserialize", array_unique(array_map("serialize", $users)));

print_r($unique_users);

?>

The output result is:

Array
(
    [id1] => Array
        (
            [name] => John
            [email] => john@example.com
        )

    [id2] => Array
        (
            [name] => Peter
            [email] => peter@example.com
        )

    [id3] => Array
        (
            [name] => David
            [email] => david@example.com
        )

    [id5] => Array
        (
            [name] => Mike
            [email] => mike@example.com
        )

)

We can see that when specifying "name" The element "id4" has been deleted, retaining the first copy that appeared in the associative array.

  1. Remove duplicate values ​​from multi-dimensional arrays

In addition to index arrays and associative arrays, we can also use the array_unique() function to remove duplicate values ​​from multi-dimensional arrays in PHP Duplicate value. For multi-dimensional arrays, you need to pay attention to the location of the specified key values.

The following shows how to use the array_unique() function to remove duplicate values ​​in a multi-dimensional array:

<?php

$courses = array(
    array("name" => "PHP Programming", "code" => "PHP101"),
    array("name" => "Web Design", "code" => "WD101"),
    array("name" => "PHP Programming", "code" => "PHP101"), 
    array("name" => "Java Programming", "code" => "JAVA101")
);

// 指定“name”键值的位置
$unique_courses = array_map("unserialize", array_unique(array_map("serialize", array_column($courses , null , 'name'))));

print_r($unique_courses);

?>

The output result is:

Array
(
    [PHP Programming] => Array
        (
            [name] => PHP Programming
            [code] => PHP101
        )

    [Web Design] => Array
        (
            [name] => Web Design
            [code] => WD101
        )

    [Java Programming] => Array
        (
            [name] => Java Programming
            [code] => JAVA101
        )

)

We can see that specifying the "name" key At the position of the value, the element "PHP Programming" has been removed, leaving a copy of the first occurrence in the multidimensional array.

Summary

In PHP, through the array_unique() function, we can easily remove duplicate values ​​in the array. For indexed arrays, associative arrays, and multidimensional arrays, you can specify the parameters of the function according to the specific situation to achieve the best results. By learning the usage of the array_unique() function, we can better master the application of PHP language in Web development and further improve our technical level.

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