How to eliminate the same key value of array in PHP

04-12-2024

In the PHP programming language, array is a very common data type. It can store multiple data elements and access each element through a key. In the development process, we often encounter the need to eliminate the same key value from the array. This article will introduce how to implement this operation in PHP.

Understand the basic concept of PHP array In PHP, an array is an ordered collection of data, which can contain different data types, such as integer, floating point, string and Boolean. Elements of an array can be accessed using their key values. In PHP, arrays can be declared and initialized in various forms, such as using the array () function or using the [] operator to create an array.

For example, in the following code, we can create an array with five elements and initialize each element.

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

Alternatively, you can use the [] operator to initialize an array:

$numbers = [1, 2, 3, 4, 5];

In both cases, the variable $numbers will be an array of five elements, and each element can be accessed by a key value. For example, we can use $numbers[0] to access the first element.

Eliminating the same key value in PHP array Usually, when writing a PHP program, we need to deal with all kinds of data, such as querying data from a database, reading data from a file, or receiving data from a Web form. In these cases, we often encounter the need to exclude values with the same key value from the PHP array.

Fortunately, there are many ways to do this in PHP. The following are the three most commonly used methods:

2.1 using array_unique function to remove duplicates

PHP has a built-in function array_unique, which can remove duplicate values in the array, such as:

$numbers = array(1, 2, 3, 4, 5, 5, 4); $uniqueNumbers = array_unique($numbers);

In the above example, we defined an array $numbers with multiple identical key values, and used the array_unique function to duplicate it. The result is that values with the same key value will be eliminated. Eventually, $uniqueNumbers will be a new array with different key values.

2.2 use array_filter function to filter key values.

The array_filter function is used to filter elements in an array. It accepts an array containing elements and returns a new array containing only those elements that meet the specified conditions.

For example, in the following code, we define an array $numbers containing multiple identical key values, and use the array_filter function to filter out duplicate key values.

$numbers = array(1, 2, 3, 4, 5, 5, 4); $uniqueNumbers = array_filter($numbers, function ($value, $key) use ($numbers) { return array_keys($numbers, $value)[0] === $key; }, ARRAY_FILTER_USE_BOTH);

In the above code, we used an anonymous function and passed in the array $numbers and the current key value as parameters. In the function body, we use the array_keys function to get the position where the current key value first appears in the array $numbers and compare it with the current key value. True will only be returned if the two key values are the same. The new array that is finally returned is an array that only contains no duplicate key values.

2.3 using foreach loop traversal to achieve deduplication

We can use the foreach loop to traverse the array and use a temporary variable to hold non-duplicate key values. For each element, we check whether it has already appeared in the temporary variable. If it is, it is deleted from the original array, otherwise it is added to the temporary variable.

The following is an example code:

$numbers = array(1, 2, 3, 4, 5, 5, 4); $tempNumbers = array(); foreach ($numbers as $key => $value) { if (in_array($value, $tempNumbers)) { unset($numbers[$key]); } else { $tempNumbers[] = $value; }}

In the above code, we first define a temporary array $tempNumbers, and use the foreach loop to traverse the original array $numbers. For each element, we check whether it has already appeared in the temporary array $tempNumbers. If yes, use the unset function to delete it from the original array $numbers; Otherwise, add it to the temporary array $tempNumbers. Finally, the $numbers array is an array without duplicate key values.

Summary In this article, we introduce the operation of eliminating the same key value from the array in PHP programming language. By using PHP's built-in functions array_unique, array_filter and foreach loop, we can easily eliminate duplicate key values from the array. Either way, it can help developers to process the data in PHP arrays more efficiently.

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us