Skip to content

Instantly share code, notes, and snippets.

@eto4detak
Last active May 30, 2019 14:34
Show Gist options
  • Save eto4detak/49317edeeffff9876e919821bd655fdd to your computer and use it in GitHub Desktop.
Save eto4detak/49317edeeffff9876e919821bd655fdd to your computer and use it in GitHub Desktop.
php sort
<?php
function sort_nested_arrays( $array, $args = array('votes' => 'desc') ){
usort( $array, function( $a, $b ) use ( $args ){
$res = 0;
$a = (object) $a;
$b = (object) $b;
foreach( $args as $k => $v ){
if( $a->$k == $b->$k ) continue;
$res = ( $a->$k < $b->$k ) ? -1 : 1;
if( $v=='desc' ) $res= -$res;
break;
}
return $res;
} );
return $array;
}
function sort_meta_index_start($a, $b)
{
$start_sort_a = get_post_meta($a->ID, 'start_sort', 1);
$start_sort_b = get_post_meta($b->ID, 'start_sort', 1);
if(empty($start_sort_a)){
$start_sort_a = 'zzzzzzzzzzzzz';
}
if(empty($start_sort_b)){
$start_sort_b = 'zzzzzzzzzzzzz';
}
if ($start_sort_a == $start_sort_b) {
return 0;
}
return ($start_sort_a < $start_sort_b) ? -1 : 1;
}
usort($prop_selection->posts, "sort_meta_index_start");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment