Created
December 3, 2018 18:49
-
-
Save dnavarrojr/9f69b4a16c3596f8b7acf127ebf51317 to your computer and use it in GitHub Desktop.
PHP Sort an Array of Arrays by Key
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function array_sort_by_key( $array, $key ) { | |
$compare = make_key_cmp( $key ); | |
usort( $array, $compare ); | |
return $array; | |
} | |
function make_key_cmp( $key ) { | |
$code = "if (\$a['$key'] == \$b['$key']) return 0;"; | |
$code .= "return (\$a['$key'] < \$b['$key']) ? -1 : 1;"; | |
return create_function( '$a,$b', $code ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment