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.
- How To Use Php Conditional Statement Example
- How to Check if String Contains Regex in PHP?
- PHP Array_column() Function Example
- PHP Generate List of Random Numbers Between 0 and 100 Example
- How to User Login with Facebook in PHP?
- How to Explode Every Character into Array Using PHP Example?
- PHP Form Validation Tutorial
- PHP Convert XML to JSON Example