I have this array:
$routes = array(
array(
\'code\' => \'PZSA\',
\'name\' => \'PLaza san antonio\',
),
array(
\'code\' => \'AVAD\
Rather than doing what is essentially a linear search for each element, it might be quickest to re-index your original array by code:
// create the index
$indexed = array();
foreach($routes as $route) {
$indexed[$route['code']] = $route;
}
// add each target to a new sorted array in order
$sorted = array();
foreach($target as $code) {
$sorted[] = $indexed[$code];
}
A little more generic solution where you can just set the key you wish to sort by and it should work for any multidimensional array.
//key to sort by
$sortBy = 'code';
$sorted = array();
foreach($routes as $route) {
foreach($route as $key => $value) {
if(!isset($sorted[$key])) {
$sorted[$key] = array();
}
$sorted[$key][] = $value;
}
}
array_multisort($sorted[$sortBy], SORT_ASC, $routes);
print_r($routes)
That's what array_multisort
if for:
foreach ($routes as $key => $row) {
$code[$key] = $row['code'];
$name[$key] = $row['name'];
}
array_multisort($code, SORT_ASC, $name, SORT_ASC, $routes);
print_r( $routes );
That way you don't even need a second array!
In fact in your case you need only to sort the codes so this will do the trick:
foreach ($routes as $key => $row) {
$code[$key] = $row['code'];
}
array_multisort($code, SORT_ASC, $routes);
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
$target_flip = array_flip($target);
usort($routes, function($a, $b) use ($target_flip){
return ($target_flip[$a['code']] < $target_flip[$b['code']]) ? -1 : 1;
});
Demo:
Docs: