一、问题
从 Redis 中取出的 hvals 值排序是随机的(参看:https://github.com/phpredis/phpredis#hvals The order is random and corresponds to redis' own internal representation of the set structure.),导致前端显示与从数据库取出的值不一致,所以把hvals的值进行降序。
二、方法
主要用到PHP的array_multisort — 对多个数组或多维数组进行排序
$unread_notify = $this->redis->hvals('ushark:unread:notify:3'); foreach ($unread_notify as &$val) { $val = json_decode($val, true); } $time = array_column($unread_notify, 'newest_time'); array_multisort($time, SORT_DESC, $unread_notify); !!! 关键 !!! print_r($unread_notify);