Home Backend Development PHP Problem How to remove identical key names in an array in php

How to remove identical key names in an array in php

Apr 25, 2023 am 10:42 AM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1252
29
C# Tutorial
1226
24