Predis - how to use an array with zadd function?

I just started using Predis to migrate Redis, and I am unable to get zadd to work with the array.

This code works:

foreach ($userIndexArr as $row) {
  $usernames[] = 0;
  $usernames[] = $row['username']; 
  $result = $this->cache->zadd('@person', 0, $row['username']);
}

It does not mean:

foreach ($userIndexArr as $row) {
  $usernames[] = 0;
  $usernames[] = $row['username']; 
}
try {
    $result = $this->cache->zadd('@person', $usernames);
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}           

And the error does not occur. Any help is much appreciated!

-J

+4
source share
3 answers

I played with this, and if you are struggling with this, the following example will surely help (after the redis.io examples ):

$predis->zadd( 'myset', [ "one" => 1, "uno" => 1, "two" => 2, "three" => 3 ] )

this will result in the same sorted set as the redis example:

ZADD myzset 1 "one"
ZADD myzset 1 "uno"
ZADD myzset 2 "two" 3 "three"

the tricky part of this, if you want to do it on one line in Redis, you first give ratings, for example:

ZADD myzset 1 "one" 1 "uno" 2 "two" 3 "three"

in Predis, this will work too:

$predis->zadd( 'myset', 1, "one", 1, "uno", 2, "two", 3, "three" );
+1
source

, : https://github.com/nrk/predis/blob/v1.0/src/Command/ZSetAdd.php

foreach ($userIndexArr as $row) {
    $usernames[$row['username']] = 0; 
}
try {
    $result = $this->cache->zadd('@person', $usernames);
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

. , , v1.0 =)

0

When using predis, you must send the member as the key, and the account as the value

$predis->zadd('your:table', array('member' => 'score');

for examples in redis docs this would be:

$predis->zadd('myzset', array('uno' => 1, 'two' => 2);
0
source

All Articles