1

I use redis (predis/predis composer) to save some id in php like this:

$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));

and i retrieve this array like this:

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

But mailIds variable is string and not an array like this:

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

Is way can access mailIds items ?

1
  • you need to use "json_decode". it will recreate an array from your JSON-string. JSON is always a string describing an array/object. Commented Jun 16, 2017 at 9:28

1 Answer 1

3

You need to parse the json (a string) so that you get an array in javascript / node.js:

client.get('mail', function(err, mailIds) {
    // parse the json
    mailIdsArray = JSON.parse(mailIds);
    console.log(mailIdsArray);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.