Skip to content

Instantly share code, notes, and snippets.

@fokosun
Forked from mchelen/index.php
Created October 25, 2018 12:42
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 fokosun/5fd5eee4fe4ac0a7e0555c82492bf0d5 to your computer and use it in GitHub Desktop.
Save fokosun/5fd5eee4fe4ac0a7e0555c82492bf0d5 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