Skip to content

Instantly share code, notes, and snippets.

@brothertao
Last active September 8, 2015 11:42
Show Gist options
  • Save brothertao/d9de53cb43752f38a6cf to your computer and use it in GitHub Desktop.
Save brothertao/d9de53cb43752f38a6cf to your computer and use it in GitHub Desktop.
php sort multidimension array by field
<?php
function msort(&$data, $field, $order='asc') {
return usort($data, function($a, $b) use ($field, $order) {
return $order==='asc' ? $a[$field] < $b[$field] : $a[$field] > $b[$field];
});
}
//example
$data = array(array('t'=>1, 'p'=>'aBc'), array('t'=>3, 'p'=>'aBb'));
$rzt = msort($data, 't', 'asc');
var_dump($data, $rzt);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment