predisHow do I use the PHP Redis zrevrange command?
The PHP Redis zrevrange command is used to get the elements of a sorted set with a score in a given range, sorted in descending order.
Example code
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->zadd('myzset', 1, 'one');
$redis->zadd('myzset', 2, 'two');
$redis->zadd('myzset', 3, 'three');
$values = $redis->zrevrange('myzset', 0, 1);
Output example
Array
(
[0] => three
[1] => two
)
Code explanation
$redis = new Redis();
- Create a new Redis instance.$redis->connect('127.0.0.1', 6379);
- Connect to Redis server.$redis->zadd('myzset', 1, 'one');
- Add an element to a sorted set, with a score of 1.$redis->zadd('myzset', 2, 'two');
- Add another element to the same sorted set, with a score of 2.$redis->zadd('myzset', 3, 'three');
- Add a third element to the same sorted set, with a score of 3.$values = $redis->zrevrange('myzset', 0, 1);
- Get the elements of the sorted set with a score between 0 and 1, sorted in descending order.
Helpful links
More of Predis
- How can I use the zscan command in PHP with Redis?
- How can I use PHP to increment values in Redis using ZINCRBY?
- How can I use Predis with a cluster in PHP?
- How can I use PHP and Redis to retrieve a range of values from a sorted set?
- How can I use PHP and Redis to get a reverse range of scores?
- How can I use Redis with the Yii PHP framework?
- How do I install and configure a PHP Redis DLL on a Windows machine?
- How do I install PHP Redis on Ubuntu 20.04?
- How can I use PHP and Redis to retrieve data from a sorted set using ZRANGEBYSCORE?
- How do I use PHP and Redis together to create a transaction?
See more codes...