Php Array Get Duplicate Values

03-Apr-2023

.

Admin

Php Array Get Duplicate Values

Hi guys,

Today i will explained to how to get duplicate values in php array. This example is so easy to use in php. THis example to array_unique() & array_diff() function to use.

Array_unique() function is provides to the php. Array_unique() function is not add in array value and second function is array_diff() function is a compare the two array and return to the difference value in array.

So let's start to the example and follow to the my all step.

Solution


$withoutDuplicates = array_unique(array_map("strtoupper", $language));

$duplicates = array_diff($language, $withoutDuplicates);

Example :

This exampel to i will used to 5 key & value array create.

<?php

$language = array(1=>'laravel',2=>'php',3 =>'php' ,4=>'java', 5=>'odo');

$withoutDuplicates = array_unique(array_map("strtoupper", $language));

$duplicates = array_diff($language, $withoutDuplicates);

print_r($duplicates);

?>

Output :

Array ( [1] => laravel [2] => php [3] => php [4] => java [5] => odo )

So, finally we are done with our code we can get below output.

#PHP 8

#PHP