Skip to content

Instantly share code, notes, and snippets.

@mchelen
Last active November 20, 2021 18:01
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mchelen/4a4cb554141dd9d181d3787b9a325a62 to your computer and use it in GitHub Desktop.
Save mchelen/4a4cb554141dd9d181d3787b9a325a62 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";
}
@fokosun
Copy link

fokosun commented Oct 25, 2018

Thank you!

@mehtacharvi
Copy link

thank you.

@davdav2008
Copy link

ty!

Copy link

ghost commented Sep 13, 2019

thanks =)

@rakeshjack
Copy link

Thank you Bro.!!!

@sainivipin
Copy link

sainivipin commented May 20, 2020

Thank you.
You are great!

@mogbril
Copy link

mogbril commented Jul 14, 2020

Thanks so much!!

@elvis-zunde
Copy link

Thanks a lot!

@a7med404
Copy link

Thanks so much!!
You are great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment