array("userI-6ren">
gpt4 book ai didi

php - 修改通过 PHPRedis 管道返回的数组以包含键作为每个数组元素的索引

转载 作者:可可西里 更新时间:2023-11-01 11:24:30 26 4
gpt4 key购买 nike

我在我的应用程序中使用 phpredis,我有以下数据结构。帐户 ID 充当每个用户的 key :

    $data = array(
"accId1"=> array("userId" => "user0234", "username" => "apples", "appversion" => "1.0"),
"accId2"=> array("userId" => "user2342", "username" => "roses", "appversion" => "2.0")
....
);

为了将以上内容存储在 Redis 中,我使用如下管道:

$pipeline = $redis->multi(Redis::PIPELINE);
foreach ($data as $accId => $userInfo) {
$pipeline->hMSet($accId, $userInfo);
}
$pipeline->exec();

用于检索:

$accIdSet = getAccountIds();
$pipeline = $redis->multi(Redis::PIPELINE);
foreach ($accIdSet as $accId) {
$pipeline->hMGet($accId, array("userId", "username", "appversion"));
}
return $pipeline->exec();

这将返回以下数组:

(
[0] => Array
(
[userId] => user0234
[username] => apples
[appverion] => 1.0
)

[1] => Array
(
[userId] => user2342
[username] => roses
[appversion] => 2.0
)
)

这一切都很好,除了数组的索引。我正在使用的系统要求索引是存储在 Redis 中的实际键,而不是它当前拥有的数字索引。

现在我知道我可以迭代这个数组并使用一些 PHP 将索引更改为实际键,但在去那里之前,我想知道我是否有更有效和更清晰的选择来解决这个问题。

我愿意接受任何建议,即使它改变了我用来与 Redis 交互的功能。谢谢!

最佳答案

我也遇到了这个问题,好像redis自己直接resovle就可以了。也许这是一个解决方法:

$data = $pipeline->exec();
foreach($data as $k => $v) {
$data[$k]['accId1'] = $accIdSet[$k];
}
return $data;

关于php - 修改通过 PHPRedis 管道返回的数组以包含键作为每个数组元素的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51141618/

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