您可以使用separate 用户元密钥:
x1, ..., x6, points
而不是阵列:
achievements
简化
WP_User_Query()
使用,不必将序列化数组作为用户元值处理。
但我想您已经考虑过这种设置,并希望避免;-)
折衷的办法是只移动points
超出achievements
数组转换为一个单独的元密钥。
Update:
当用户数量相对较少时,我们可以使用以下(未经测试的)PHP排序:
$users2points = array();
$args = array(
\'meta_key\' => \'achievements\',
\'number\' => $no,
\'offset\' => $offset
);
$users = get_users( $args );
foreach( (array) $users as $user )
{
$meta = get_user_meta( \'achievements\', $user->ID );
if( isset( $meta[\'points\'] )
$users2points[$user->ID] = $meta[\'points\'];
}
asort( $users2points, SORT_NUMERIC );
print_r( $users2points );
Update 2:
下面是另一个更冒险的PHP排序,它将直接在
$users
的数组
WP_User
对象:
$args = array(
\'meta_key\' => \'achievements\',
\'number\' => $no,
\'offset\' => $offset
);
$users = get_users( $args );
foreach( (array) $users as $user )
{
$meta = get_user_meta( \'achievements\', $user->ID );
// Here we assume the points values are greater or equal to 0:
$user->points = ( isset( $meta[\'points\'] ) ? $meta[\'points\'] : 0;
}
/*
usort( $users, function( $a, $b ) {
return gmp_cmp( $a->points, $b->points );
});
*/
// The gmp_cmp() might suffer from a memory leak,
// according to some PHP bug reports,
// so let\'s use this instead:
usort( $users, function( $a, $b ) {
if( $a->points === $b->points )
return 0;
else
return ( $a->points > $b->points ) ? 1 : -1;
});
print_r( $users );