

Check if array contains duplicates in PHP
source link: https://thispointer.com/check-if-array-contains-duplicates-in-php/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Check if array contains duplicates in PHP
This tutorial will discuss about a unique way to check if an array contains duplicates elements in php.
To check if an array contains any duplicate element or not we are going to use the array_unique()
function.
The array_unique()
function accepts an array as an argument and return a new array containing only unique elements from the given array. Basically the return array will not have any duplicate elements.
So we can pass over our original array to the array_unique()
function and compare the size of the returned array with the size of original array. If the size of both the arrays are equal then it means the original array has no duplicate element. Otherwise, if original array has more elements, then it means that the original array has duplicate elements.
Let’s see the complete example,
Advertisements
<?php $arr = array(23, 89, 16, 11, 21, 89, 90); // Get a copy of unique elements from array $uniqArray = array_unique($arr); // If size of both the array is equal, // then it means array has no duplicates if (count($uniqArray) < count($arr)) { echo "Yes, the array have some duplicate elements"; } else { echo "No, the array does not contain any duplicate element"; } ?>
Output
Yes, the array have some duplicate elements
Summary
We learned a way to check if an array contains duplicate elements in PHP.
Recommend
-
7
In this article, We will learn how to check if a NumPy Array contains any NaN value in Python. Table of Contents What is a NaN value? The NaN stands for Not a Number, which is a numeric data type that can be interpre...
-
3
In this article, we will discuss different ways to check if a C++ array contains duplicates or not. Table Of Contents Problem Description We have given an array that contains n numb...
-
8
This tutorial will discuss about unique ways to check if a NumPy Array contains a specific string in Python. Table Of Contents Technique 1: Using “in” Keyword Python provid...
-
5
This tutorial will discuss about unique ways to check if a numpy array has duplicates in Python. Table Of Contents Method 1: Using numpy.unique() The numpy modul...
-
14
This tutorial will discuss about unique ways to check if numpy array contains only empty strings. Table Of Contents Method 1: using numpy.char.str_len() The numpy
-
11
This tutorial will discuss about unique ways to check if an array contains a key in php. Table Of Contents Method 1: Using array_key_exists() function The
-
7
This tutorial will discuss about unique ways to check if an array contains a value in php. Table Of Contents Method 1: Using in_array() function The in_array()
-
9
This tutorial will discuss about unique ways to check if an array contains any value from another array in php. Table Of Contents Method 1: Using array_intersect() funct...
-
8
This tutorial will discuss about unique ways to check if any string an array contains a substring in PHP. Table Of Contents Method 1: Using a Regex Pattern The preg...
-
11
This tutorial will discuss about unique ways to check if array contains all values from another array in php. Table Of Contents Method 1: Using array_diff() function ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK