PHP Check If Array Has Duplicate Values Example

By Hardik Savani May 14, 2024 Category : PHP

Hey Folks,

Here, I will show you how to work php check if array has duplicate values. you can understand a concept of php check if array has same values. you'll learn php check if arrays have same values. If you have a question about check if array contains duplicate values php laravel then I will give a simple example with a solution.

we will use count() function and array_unique() function to check check if array has duplicate values in php. so, let's see the simple code of how to check duplicate values in array php.

Example:

index.php

<?php

$myArray = ['One', 'Two', 'Three', 'Two', 'Five', 'Three', 'One'];

if (count($myArray) !== count(array_unique($myArray))){

var_dump("Array has duplicate value.");

} else {

var_dump("Array dose not have duplicate value.");

}

?>

Output:

string(26) "Array has duplicate value."

I hope it can help you...

Tags :
Shares