Skip to content

Instantly share code, notes, and snippets.

@sufimalek
Created July 15, 2019 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sufimalek/6d0fcbc89699c43688e5570b94f45e7e to your computer and use it in GitHub Desktop.
Save sufimalek/6d0fcbc89699c43688e5570b94f45e7e to your computer and use it in GitHub Desktop.
php check multidimensional array for duplicate values
<?php
$arrays = array(
array(
'name'=>'foo',
),
array(
'name'=>'bar',
),
array(
'name'=>'foo',
),
array(
'name'=>'foo',
'type'=>'baz',
),
);
echo "find completely identical arrays\n";
foreach ($arrays as $current_key => $current_array) {
$search_key = array_search($current_array, $arrays);
echo "current key: $current_key \n";
echo "search key: $search_key \n";
if ($current_key != $search_key) {
echo "duplicate found for item $current_key\n";
}
echo "\n";
}
echo "\n\nfind arrays with duplicate value for 'name'\n";
foreach ($arrays as $current_key => $current_array) {
echo "current key: $current_key\n";
foreach ($arrays as $search_key => $search_array) {
if ($search_array['name'] == $current_array['name']) {
if ($search_key != $current_key) {
echo "duplicate found: $search_key\n";
}
}
}
echo "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment