Home Daily Programming PHP Knowledge How to remove duplicate elements from an array in PHP? (Pictures + Videos)

How to remove duplicate elements from an array in PHP? (Pictures + Videos)

Sep 28, 2018 pm 04:30 PM

This article mainly introduces to you the detailed explanation of PHP's specific method of deleting duplicate elements in an array.

PHP performs basic operations such as deleting, querying, and adding elements in an array. It can be said that it is a relatively common question in PHP interview questions. I believe that for most PHP scholars, these are very simple knowledge points. But for novices, it may not be clear.

Below we will introduce in detail two methods of deleting duplicate elements from PHP arrays through simple code examples.

Method 1: Remove array duplication through the array_unique function

<?php
$arr = [&#39;1&#39;, &#39;1&#39;, &#39;PHP&#39;, &#39;PHP&#39;, 2, 3];
print_r($arr);
echo "<br>";
print_r(array_unique($arr));
Copy after login

Here we define an array $arr, and you can find that there are 2 duplicate elements in the array , and then use the array_unique function to remove duplicate elements.

Accessed through the browser, the return value result is as shown below:

How to remove duplicate elements from an array in PHP? (Pictures + Videos)

From the picture we can find that the repeated elements in the original array have been removed through the array_unique function Delete it, and keep the remaining key names unchanged.

array_unique: Remove duplicate values ​​in the array

Note: Key names are retained constant.

Method 2: Remove array duplication through the array_flip function

<?php
$arr = [&#39;1&#39;, &#39;1&#39;, &#39;PHP&#39;, &#39;PHP&#39;, 2, 3];
print_r($arr);
echo "<br>";
print_r(array_unique($arr));
echo "
"; print_r(array_flip($arr));
Copy after login

Here we combine the array_unique method for comparison. The test results are as follows:

How to remove duplicate elements from an array in PHP? (Pictures + Videos)

As shown in the figure, the array_flip function can also be used to deduplicate the array, but unlike array_unique, the array_flip function method causes the key names to be exchanged after deduplication.

array_flip: Exchange the keys and values ​​in the array

Note: If the same value appears multiple times times, the last key name will be used as its value, and the other keys will be discarded.

This article is about two methods of PHP deleting duplicate elements in arrays. I hope it will be helpful to friends in need!

If you want to learn more about PHP, you can follow the PHP Chinese website PHP Video Tutorial, everyone is welcome to refer to and learn!

The above is the detailed content of How to remove duplicate elements from an array in PHP? (Pictures + Videos). 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)