Sort multidimensional array by certain key

Sorts multidimensional array by any key using usort() function. Read more.

php
fork add to bundle source
function multi_sort_array( &$array, $key, $desc = true ) { $handle = function($a, $b) use( $key, $desc) { return !$desc ? $a[$key] > $b[$key] : $a[$key] < $b[$key]; }; usort($array, $handle); }

usage

load_function('multi_sort_array'); $array = [ 0 => ['name' => 'Dan'], 1 => ['name' => 'Alex'], 2 => ['name' => 'John'] ]; multi_sort_array($array, 'name'); print_r($array); // Array // ( // [0] => Array // ( // [name] => John // ) // [1] => Array // ( // [name] => Dan // ) // [2] => Array // ( // [name] => Alex // ) // )
← how to use this in app?
create log ...inout