5

can predis use array as 2nd parameter of hmget() to retrieve multiple fields on one go? e.g. $client->hmget($key, $fields); //$fields is an array

Can it also accept many parameters of string as fields? e.g.: $client->hmget($key, $field1, $field2, $field3);

j0k
  • 22,600
  • 28
  • 79
  • 90
Will-i-Am-Davidon
  • 1,532
  • 1
  • 14
  • 15

3 Answers3

8

Predis supports two ways of passing multiple keys (or keys with values) for variadic Redis commands. The first one basically follows the same signature of commands as defined by the Redis documentation, so using HMSET and HMGET as examples you will have:

$redis->hmset("hash", "field:1", "value:1", "field:2", "value:2");
$values = $redis->hmget("hash", "field:1", "field:2");

but you can also pass the list of keys and/or values as a single array argument:

$redis->hmset("hash", array("field:1" => "value:1", "field:2" => "value:2"));
$values = $redis->hmget("hash", array("field:1", "field:2"));

Choosing which one to use is really just a matter of preference.

nrk
  • 878
  • 8
  • 10
  • Sorry @nrk, only the second approach works for me. The documentation https://github.com/phpredis/phpredis#hmget suggests its an array. The first approach returns a false. Is it dependent on a specific version? – Sanath Ballal May 05 '17 at 03:16
  • @SanathBallal phpredis is not Predis, the question (and my answer) was related to the latter. – nrk May 17 '17 at 10:01
1

From Predis repository

$redis->hmset('metavars', 'foo', 'bar', 'hoge', 'piyo', 'lol', 'wut');

$redis->hmget('metavars', 'foo', 'hoge', 'unknown'));
Jack Daniel's
  • 2,583
  • 22
  • 28
0

You can try this:

$pd =  $redis->hmget("regconfig" ,array("cont_mgr"));
print_r($pd);
Pankaj Chauhan
  • 1,623
  • 14
  • 12