To Check Duplicate Values in Foreach PHP, we use an example where we find duplicate values in an array without using PHP inbuilt function, and then on the second method, we find duplicate values in an associative array with the help of array_search()
function.
Here we discuss two methods to find duplicate values in array using PHP foreach loop,
- Using
array_search()
function and foreach loop. - Using two foreach loops.
In this process, first, we create an associative array with static values, then we do both operations where we use foreach loop.
Example of Find Duplicate Values in Array PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
$arrays = array( array( 'name'=>'Lorem', ), array( 'name'=>'ipsum', ), array( 'name'=>'Lorem', ), array( 'name'=>'Lorem', 'type'=>'bar', ), ); echo "<br>find arrays with duplicate value for 'name'<br>"; foreach ($arrays as $current_key => $current_array) { echo "current key: $current_key<br>"; foreach ($arrays as $search_key => $search_array) { if ($search_array['name'] == $current_array['name']) { if ($search_key != $current_key) { echo "duplicate found: $search_key<br>"; } } } echo "<br>"; } |
Code Explanations:
- On the above code, we create an associative array then we use two foreach loops to find duplicate values in array PHP.
- We use the “name” key to check the duplicate values in the array.
- When the first loop starts it runs according to array size and checks first value with all other values with the help of the second foreach loop.
Output:
find arrays with duplicate value for ‘name’
current key: 0
duplicate found: 2
duplicate found: 3current key: 1
current key: 2
duplicate found: 0
duplicate found: 3current key: 3
duplicate found: 0
duplicate found: 2
It also works for Find Duplicate Value in Foreach PHP Without Using Function this condition.
Find Duplicate Values Using Array Search Function PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
$arrays = array( array( 'name'=>'Lorem', ), array( 'name'=>'ipsum', ), array( 'name'=>'Lorem', ), array( 'name'=>'Lorem', 'type'=>'bar', ), ); foreach ($arrays as $current_key => $current_array) { $search_key = array_search($current_array, $arrays); echo "current key: $current_key <br>"; echo "search key: $search_key <br>"; if ($current_key != $search_key) { echo "duplicate found for item $current_key<br>"; } echo "<br>"; } |
Code Explanations:
- Here we also use the same associative array which we use on the above code.
- Here we only check with the array key and use
array_search
function to get all the array keys and match with others using foreach loop one by one.
Output:
current key: 0
search key: 0current key: 1
search key: 1current key: 2
search key: 0
duplicate found for item 2current key: 3
search key: 3
Here is the complete source code to check duplicate values in an associative array with and without using PHP inbuilt function. If you get any issue with the code, you can comment below.
You can also use the array unique function. To know more you can check the official website https://www.php.net/manual/en/function.array-unique.php
Also Check:
- Get List of Holidays Using Google Calendar API
- Image Creation From User Input in PHP
- Allow Only String and Numbers Using PHP
- Migrate WordPress Site To New Host Manually
Happy Coding..!
2 Replies to “Check Duplicate Values in Foreach PHP”