I am following this thread How can I sort arrays and data in PHP?
I want to sort a two dimensinal array based on another single dimensional array
array i want to sort
$main = array(array('name' => 'terry', 'age' => '25', 'sex' => 'm', 'status' => 'active'),
array('name' => 'raul', 'age' => '26', 'sex' => 'm', 'status' => 'active'),
array('name' => 'mata', 'age' => '27', 'sex' => 'm', 'status' => 'active'),
array('name' => 'ronaldo', 'age' => '28', 'sex' => 'm', 'status' => 'active'),
array('name' => 'drogba', 'age' => '29', 'sex' => 'm', 'status' => 'active'),
array('name' => 'messi', 'age' => '30', 'sex' => 'm', 'status' => 'active'));
order array
$order = array('30','25');
function cmp(array $a, array $b) {
global $order;
return array_search($a['age'], $order) - array_search($b['age'], $order);
}
usort($main, 'cmp');
This is the function i am trying out. but it is not sorting the array.
desired output: I want to get the arrays with age value 30 & 25 as the first two arrays in my input two dimensional array
I am able to do it with this code
function cmp(array $a, array $b) {
global $order;
foreach ($order as $ord) {
if (in_array($ord, $a)) {
return true;
}
}
}
But i want to avoid the foreach loop