Skip to content

Instantly share code, notes, and snippets.

@kagg-design
Created November 12, 2021 18:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kagg-design/58d545eb48858498d49f3ae358e43cf1 to your computer and use it in GitHub Desktop.
Save kagg-design/58d545eb48858498d49f3ae358e43cf1 to your computer and use it in GitHub Desktop.
uasort example
<?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