neo115162 Posted October 22, 2012 Share Posted October 22, 2012 I have a multidimensional array as follows below: $_ARRAY=array( array("a"=>"File1.xml","b"=>1111,"c"=>"1"), array("a"=>"File1.xml","b"=>1111,"c"=>"2"), array("a"=>"File2.xml","b"=>2222,"c"=>"1"), array("a"=>"File2.xml","b"=>2222,"c"=>"1"), array("a"=>"File3.xml","b"=>3333,"c"=>"1"), array("a"=>"File3.xml","b"=>1111,"c"=>"2") ); As you can tell from above there are 2 duplicates (File1.xml B and C combination is the same as File3.xml B and C combination) and I would like to have a check that shows duplicates with the file name that is associated with those duplicates. Any help is greatly appreciated. Much thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/269775-unique-multidimensional-array/ Share on other sites More sharing options...
ManiacDan Posted October 22, 2012 Share Posted October 22, 2012 I can't believe there's not a custom-unique function...weird. This is a handshake problem. A quick and dirty solution to it is: foreach ( $array as $key => $val ) { foreach ( $array as $key2 => $val2 ) { if ( $key == $key2 ) continue; if ( $val['b'] == $val2['b'] && $val['c'] == $val2['c'] ) { //the current pair is a duplicate according to your criteria } } } Note that this is inefficient, for loops would be better, but this is easier to understand. Quote Link to comment https://forums.phpfreaks.com/topic/269775-unique-multidimensional-array/#findComment-1386975 Share on other sites More sharing options...
Christian F. Posted October 22, 2012 Share Posted October 22, 2012 Would this data be fetched from a database, by any chance? If so, then you can ask the DB to give you the duplicates. You just need GROUP_CONCAT(), COUNT() and HAVING to accomplish it. Quote Link to comment https://forums.phpfreaks.com/topic/269775-unique-multidimensional-array/#findComment-1387037 Share on other sites More sharing options...
neo115162 Posted October 22, 2012 Author Share Posted October 22, 2012 Thank you very much guys, I really really appreciate it! Quote Link to comment https://forums.phpfreaks.com/topic/269775-unique-multidimensional-array/#findComment-1387072 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.