gpt4 book ai didi

php - Predis - 如何使用带有 zadd 函数的数组?

转载 作者:IT王子 更新时间:2023-10-29 05:59:42 27 4
gpt4 key购买 nike

我刚开始使用 Predis 进行 Redis 迁移,但我无法让 zadd 函数处理数组。

此代码有效:

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

这不是:

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";
}

并且不会抛出任何错误。非常感谢任何帮助!

-J

最佳答案

我试过这个,如果你正在努力解决这个问题,下面的例子肯定会有所帮助(在 redis.io documents 例子之后):

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

这将产生与 redis 示例相同的排序集:

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

如果你想在 Redis 中的一行中执行此操作,那么棘手的部分是将分数放在第一位,如下所示:

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

在 Predis 中,这也可以工作:

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

关于php - Predis - 如何使用带有 zadd 函数的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26477074/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com