How to sort an array of arrays in php?

Solution 1:

You can use usort function. Since PHP 5.4 you can use closure function:

usort($mainArray, function ($a, $b) {
  $a_val = (int) $a['order'];
  $b_val = (int) $b['order'];

  if($a_val > $b_val) return 1;
  if($a_val < $b_val) return -1;
  return 0;
});

Or version for PHP < 5.4:

usort($mainArray, 'myCompare');

function myCompare($a, $b) {
  $a_val = (int) $a['order'];
  $b_val = (int) $b['order'];

  if($a_val > $b_val) return 1;
  if($a_val < $b_val) return -1;
  return 0;
}