Phpredis: How to use ZADD to set multiple scores & values?

Created on 3 Feb 2012  路  4Comments  路  Source: phpredis/phpredis

zadd support multiple scores & values via redis.io: ZADD key score member [score] [member]
but i tried : $redis->zadd($key, array(1,2,3), array('a', 'b', 'c')), and it doesn't work, the result as
Array
(
[Array] => 1
)
and i also tried :
$params = array($k);
$s = (array)$s;
$m = (array)$m;
foreach ($s as $n => $i)
{
$params[] = $i;
$params[] = $m[$n];
}
return call_user_func_array(array('parent', 'zAdd'), $params);

and it doesn't work too
so if phpredis support this feature, could u pls show me how to use it
thx

Most helpful comment

hi @RobChen,
Redis::zAdd accepts following signature when you passing multiple elements.

Redis::zAdd(key, score, value[, score, value...])

  • score should be integer.
  • value should be string.

for example:

<?php
r = new Redis();
$r->connect("localhost");

$key = "zadd_test";

$scores = array(1,2,3,4);
$values = array('a','b','c','d');

$args = array();
$length = count($scores);
$args[] = $key;
for ($i = 0; $i < $length; $i++) {
  $args[] = $scores[$i];
  $args[] = $values[$i];
}

call_user_func_array(array($r,'zAdd'),$args);

you know redis-server must be 2.4 higher when you want to set multiple scores.

All 4 comments

hi @RobChen,
Redis::zAdd accepts following signature when you passing multiple elements.

Redis::zAdd(key, score, value[, score, value...])

  • score should be integer.
  • value should be string.

for example:

<?php
r = new Redis();
$r->connect("localhost");

$key = "zadd_test";

$scores = array(1,2,3,4);
$values = array('a','b','c','d');

$args = array();
$length = count($scores);
$args[] = $key;
for ($i = 0; $i < $length; $i++) {
  $args[] = $scores[$i];
  $args[] = $values[$i];
}

call_user_func_array(array($r,'zAdd'),$args);

you know redis-server must be 2.4 higher when you want to set multiple scores.

Thanks @chobie!

Thanks @chobie
i found my phpredis version is 2 low to cause that problem
thx

@chobie good!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

coolrecep picture coolrecep  路  3Comments

xkenner picture xkenner  路  3Comments

marius-meissner picture marius-meissner  路  5Comments

fidelhuang picture fidelhuang  路  5Comments

andyli029 picture andyli029  路  5Comments