I am trying to sort an array based on the first [0] child element. But in my code the keys are being replaced by the array number in the sort:
$myArray = array(
'my last row' => array(
'0' => 'ZZZZ',
'1' => 'AAAA'
),
'the first row' => array(
'0' => 'AAAA'
)
);
usort($myArray, 'cmp' ) ;
var_dump($myArray);
function cmp ($a, $b) {
return ( ( $a[0] > $b[0] ) ? 1 : -1 );
}
result:
array(2) {
[0]=> // should be ['the first row'] *not* [0]
array(1) {
[0]=>
string(4) "AAAA"
}
[1]=> // should be ['my last row'] *not* [1]
array(2) {
[0]=>
string(4) "ZZZZ"
[1]=>
string(4) "AAAA"
}
}
The sort order itself appears to be working as expected.
I would like to see the following:
the first row => AAAA
my last row => ZZZZ, AAAA
This is probably a very simple issue, but I cannot resolve it.
Thank you very much.
EDIT: this sort does not involve the key itself, but rather a child-element key. i believe that makes this question unique.