I have this array:
$routes = array(
array(
\'code\' => \'PZSA\',
\'name\' => \'PLaza san antonio\',
),
array(
\'code\' => \'AVAD\
Alternatively to the answers already mentioned (With the exception of Peter), you can use your logic with uasort()
. You'll first have to define a comparison function (compare $a to $b), and build in logic using the values -1
, 0
, and 1
to decide whether the compared row should go before, after or stay the same.
// This sorts simply by alphabetic order
function reindex( $a, $b )
{
// Here we grab the values of the 'code' keys from within the array.
$val1 = $a['code'];
$val2 = $b['code'];
// Compare string alphabetically
if( $val1 > $val2 ) {
return 1;
} elseif( $val1 < $val2 ) {
return -1;
} else {
return 0;
}
}
// Call it like this:
uasort( $routes, 'reindex' );
print_r( $routes );
Of course, that only works as a small example, and if you are trying to index them in alphabetical order. If you need it to sort by an exact set of keys, not in alphabetical order, well it might be a bit trickier, as it doesn't accept any other parameters.
PHP uasort() Reference