2017-06-16 51 views
1

我使用redis(predis/predis作曲家)以节省一些ID在PHP这样的:如何将php数组存储到redis中并在nodejs中检索?

$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项目?

+0

你需要使用 “json_decode”。它会从你的JSON字符串中重新创建一个数组。 JSON始终是描述数组/对象的字符串。 – wayneOS

回答

3

您需要解析JSON(字符串),让你的JavaScript/node.js中得到一个数组:

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