Created
November 12, 2021 18:25
-
-
Save kagg-design/58d545eb48858498d49f3ae358e43cf1 to your computer and use it in GitHub Desktop.
uasort example
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
<?php | |
$arr = [ | |
[ 'foo' => 1, 'bar' => 7, 'den' => 9 ], | |
[ 'foo' => 11, 'bar' => 4, 'den' => 0 ], | |
[ 'foo' => 1, 'bar' => 12, 'den' => 6 ], | |
[ 'foo' => 1, 'bar' => 10, 'den' => 6 ], | |
]; | |
uasort( $arr, 'cmp' ); | |
print_r( $arr ); | |
function cmp( $a, $b ) { | |
if ( $a['foo'] < $b['foo'] ) { | |
return - 1; | |
} | |
if ( $a['foo'] === $b['foo'] ) { | |
if ( $a['bar'] < $b['bar'] ) { | |
return - 1; | |
} | |
if ( $a['bar'] === $b['bar'] ) { | |
return 0; | |
} | |
return 1; | |
} | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment