How to remove identical key names in an array in php

PHPz
Release: 2023-04-25 14:08:19
Original
413 people have browsed it

In PHP programming, array is a commonly used data structure, often used to store a group of data of the same type. An array is an indexed list where each element has a specific key. But usually, we may encounter the need to remove the same key names in the array. This article will introduce how to remove the same key names in an array in PHP.

Method 1: Use the array_unique function

The array_unique() function can remove duplicate values ​​​​in the array and return a new array. If you need to remove elements with the same key names, you can first use array_values() to obtain a new index array, then use the array_unique() function to remove duplicates, and finally use the array_combine() function to recombine the key names and values ​​into a new associative array. .

The sample code is as follows:

$original_array = array(
  "id" => "001",
  "name" => "John",
  "gender" => "male",
  "id" => "002",
  "city" => "New York",
  "gender" => "female"
);

$new_array = array_values(array_unique($original_array));
$result_array = array_combine(range(1, count($new_array)), $new_array);

print_r($result_array);
Copy after login

The above code will output the following results:

Array
(
    [1] => 001
    [2] => John
    [3] => male
    [4] => New York
    [5] => female
)
Copy after login

Method 2: Use foreach loop

If we need to in the original array To deduplicate elements with duplicate key names and retain the last one, you can use a foreach loop to traverse the array, find duplicate elements by comparing key names, and then use the unset() function to delete duplicate elements.

The sample code is as follows:

$original_array = array(
  "id" => "001",
  "name" => "John",
  "gender" => "male",
  "id" => "002",
  "city" => "New York",
  "gender" => "female"
);

foreach ($original_array as $key => $value) {
  $duplicated_key = array_keys($original_array, $value);
  if (count($duplicated_key) > 1) {
    unset($original_array[$duplicated_key[0]]);
  }
}

print_r($original_array);
Copy after login

The above code will output the following results:

Array
(
    [name] => John
    [gender] => female
    [city] => New York
)
Copy after login

Note that the above code can only remove consecutive duplicate key names. If you want to remove all duplicate key names, you can change the foreach loop to a while loop, and perform the unset() operation internally until the key names of the deleted elements are no longer repeated.

To sum up, you can use the array_unique() function or foreach loop in PHP to remove the same key names in the array. In specific scenarios, we should choose the appropriate method based on the actual situation.

The above is the detailed content of How to remove identical key names in an array 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!