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 PHP and Redis to set a time-to-live (TTL) value?
- How can I use the zscan command in PHP with Redis?
- How do I install PHP, Redis, and XAMPP?
- How can I troubleshoot a "PHP Redis went away" error?
- How can I set a timeout for a Redis connection using PHP?
- 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 get keys based on a pattern?
- How can I use PHP and Redis to parse JSON data?
See more codes...