【问题标题】:how store php array to redis and retrieve in nodejs?如何将php数组存储到redis并在nodejs中检索?
【发布时间】:2017-11-19 00:22:17
【问题描述】:

我使用redis(predis/predis composer)在php中保存一些id,如下所示:

$redis = new Client();
$mailJson = $redis->get('mail');
$mail  = json_decode($mailJson);
$no = rand(100,500);
array_push($mail, $no);
$redis->set('mail', json_encode($mail));

然后我像这样检索这个数组:

var redis = require("redis"),
    client = redis.createClient();
client.on('connect', function() {
    client.get('mail', function(err, mailIds) {
        console.log(mailIds);
    });
});

但是mailIds 变量是字符串而不是这样的数组:

[1,200,500,500,400,100,200,100]

方式可以访问mailIds项目吗?

【问题讨论】:

  • 您需要使用“json_decode”。它将从您的 JSON 字符串重新创建一个数组。 JSON 始终是描述数组/对象的字符串。

标签: php node.js redis


【解决方案1】:

你需要解析json(一个字符串),以便在javascript/node.js中得到一个数组:

client.get('mail', function(err, mailIds) {
    // parse the json
    mailIdsArray = JSON.parse(mailIds);
    console.log(mailIdsArray);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-08
    • 2017-07-04
    • 2023-03-08
    相关资源
    最近更新 更多