Jump to content

Unique Multidimensional Array


neo115162

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/269775-unique-multidimensional-array/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.