I have been following the instructions for sorting an array by a value in the sub-arrays (from Sort Multi-dimensional Array by Value [duplicate]) but it's just not working for me. See my code below:
$items[] = array("apple", "green", 5.13);
$items[] = array("banana", "green", 5.03);
$items[] = array("banana", "yellow", 6.13);
$items[] = array("apple", "red", 7.13);
function sortByOrder($a, $b) {
return $a[2] - $b[2];
}
usort($items, 'sortByOrder');
foreach ($items as $item) {
echo "$item[2] : $item[0] - $item[1]\n";
}
This code returns:
5.13 : apple - green
5.03 : banana - green
6.13 : banana - yellow
7.13 : apple - red
The expected result is this:
5.03 : banana - green
5.13 : apple - green
6.13 : banana - yellow
7.13 : apple - red
So what am I doing wrong?