Question

I tried using lpush

$list = "flavors";
$array = array($_GET["mainid"], $_GET["flavor1"], $_GET["flavor2"], $_GET["flavor3"]);
$redis = new Predis\Client();
$redis->lpush($list,implode("", $array));
echo $redis->lrange(0, -1);

I have tried using hset

$redis->hset("flavors", $_GET["mainid"], $_GET["mainid"]);
$redis->hset("flavors", $_GET['mainid'] . "flavor1", $_GET["flavor1"]);
$redis->hset("flavors", $_GET['mainid'] . "flavor2", $_GET["flavor2"]);
$redis->hset("flavors", $_GET['mainid'] . "flavor3", $_GET["flavor3"]);
echo $redis->hgetall($_GET['mainid']);

But I can't get that to work because I get this error: http://pastie.org/8401717

How could I fix that? I think it is something about being given an array when it expects a string, but I have implode in there, so why else isn't it working? If it can't work at all, what other Redis data type could I use?

Était-ce utile?

La solution

You got that error when using lpush because the flavors key is already stored with a different Redis data type. So you should delete that key before you try again.

You also used lpush in wrong way. You should try this:

foreach ($array as $value) {
    $redis->lpush($list, $value);
}

Or if your redis api support multi params:

call_user_func_array(array($redis, 'lpush'), array_merge($list, $array));

If you want to store flavors by mainid, you may want to store it with multi keys and use lpush:

$list = "flavor:{$_GET['mainid']}";
$redis->lpush($list, $_GET["flavor1"]);
$redis->lpush($list, $_GET["flavor2"]);
$redis->lpush($list, $_GET["flavor3"]);

Another way is to store in a single hash and using json_encode (don't use implode):

$data = json_encode(array($_GET["flavor1"], $_GET["flavor2"], $_GET["flavor3"]));
$redis->hset('flavors', $_GET["mainid"], $data);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top