在二维数组中查找配对

I have a two-dimensional array and look for a way to find the all double entries. E.g. if the array is of the form

$a = array(
   array('a','b','c'),
   array('d','a','e'),
   array('d','c','b')
)

the function should return the list

array(array(0,0),array(1,1))  // Since $a[0,0]=$a[1,1]
array(array(0,1),array(2,2))  // Since $a[0,1]=$a[2,2]
array(array(0,2),array(2,1))  // Since $a[0,2]=$a[2,1]
array(array(1,0),array(2,0))  // Since $a[1,0]=$a[2,0]
array(1,2)                    // Unmatched

Is there an elegant/efficient way to implement this?

With a nested loop. Use the values as keys in the result and append the coordinates as arrays under those keys.

foreach ($a as $x => $inner) {
    foreach ($inner as $y => $value) {
        $result[$value][] = [$x, $y];
    }
}

The $result will contain sets of coordinates for all the given values. You can group the results by size of set to identify which values are unmatched, pairs, or have even greater than two occurrences (if that's possible).

foreach ($result as $value => $set) {
    $sets[count($set)][$value] = $set;
}