When your comparison function rcmp
is invoked, its arguments $a
and $b
are arrays so your equality and less-than operators have arrays as their operands.
The equality operator on arrays works like this:
TRUE if $a and $b have the same key/value pairs.
The less-than comparison works like this:
Array with fewer members is smaller, if key from operand 1 is not found in operand 2 then
arrays are uncomparable, otherwise - compare value by value.
"Uncomparable" means the comparison evaluates to null
, which in turn causes rcmp
to return 1
.
So what ends up happening is that when $a
and $b
are not identical arrays rcmp
always returns 1 (i.e. considers that $a
is greater). This happens irrespective of what the key in each array is, and as a result you get a meaningless ordering.
If you wanted to order these arrays by their first key with usort
, you would do it like this:
function rcmp($a, $b)
{
return key($a) - key($b);
}