Home > Backend Development > PHP Tutorial > How to remove duplicates from an array based on specific key-value pairs in PHP?

How to remove duplicates from an array based on specific key-value pairs in PHP?

WBOY
Release: 2024-04-28 18:18:01
Original
744 people have browsed it

In PHP, use the array_unique() function to remove duplicates from an array based on specific key-value pairs. When calling the function, pass in the array as a parameter and select the sorting method as the second parameter. This function returns a new array in which duplicates have been removed based on the specified key-value pairs.

如何在 PHP 中根据特定键值对去除数组中的重复项?

How to remove duplicates from an array based on specific key-value pairs in PHP

In PHP, use The array_unique() function removes duplicates from an array based on specific key-value pairs. This function receives an array as argument and returns a new array in which duplicates have been removed based on the specified key-value pairs.

Usage:

$array = [
    ['name' => 'John', 'age' => 30],
    ['name' => 'Mary', 'age' => 25],
    ['name' => 'John', 'age' => 30],
    ['name' => 'Bob', 'age' => 20],
];

$uniqueArray = array_unique($array, SORT_REGULAR);

print_r($uniqueArray);
Copy after login

Output:

Array
(
    [0] => Array
        (
            [name] => John
            [age] => 30
        )

    [1] => Array
        (
            [name] => Mary
            [age] => 25
        )

    [2] => Array
        (
            [name] => Bob
            [age] => 20
        )
)
Copy after login

As shown above, array_unique() According to Key-value pairs ['name', 'age'] Remove duplicates in the array.

Optional parameters:

array_unique() The second parameter of the function specifies how to compare array elements. The following options are available:

  • SORT_REGULAR: Compare elements normally
  • SORT_NUMERIC: Compare elements as numbers
  • SORT_STRING: Compare Elements as strings
  • SORT_LOCALE_STRING: Comparing elements as strings with a specific locale

Practical example:

Suppose you have the following array, which contains line items from different orders:

$orders = [
    ['id' => 1, 'item_id' => 1, 'quantity' => 2],
    ['id' => 2, 'item_id' => 2, 'quantity' => 1],
    ['id' => 3, 'item_id' => 1, 'quantity' => 3],
];
Copy after login

You can use the following code to sort the order based on the order item ID (item_id) and quantity (quantity) Remove duplicates:

$uniqueOrders = array_unique($orders, SORT_REGULAR);
Copy after login

This will create a new array $uniqueOrders with the item_id and quantity combination for each order item All are unique.

The above is the detailed content of How to remove duplicates from an array based on specific key-value pairs 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