Redis stored in array

295 views Asked by At

I have multiple redis instances and I want to access them through php. I am using phpredis 7. Since I have multiple functions to connect to different servers, I stored them in an array.

function ud_1() {
    $redis = new Redis();
    if ($redis->connect('127.0.0.1', 12341) == false)
        return "Cannot connect to redis(ud_1).";
    else
        return $redis;
    $type = $UD;
}
$red =[
    "ud_1" => ud_1(),
    "ud_2" => ud_2(),
    "ud_3" => ud_3()
];

The array contains:

array(3) { ["ud_1"]=> object(Redis)#1 (1) { ["socket"]=> resource(3) of type (Redis Socket Buffer) } ["ud_2"]=> object(Redis)#2 (1) { ["socket"]=> resource(5) of type (Redis Socket Buffer) } ["ud_3"]=> object(Redis)#3 (1) { ["socket"]=> resource(7) of type (Redis Socket Buffer) } } 

Now I want to use it like:

$red[0]->set("key1", "value1");

I have tried but nothing really happens. Can someone give me a hint where am I wrong?

1

There are 1 answers

0
2ps On

I think you are just accessing your array incorrectly. It looks like you have an associative array, so you access it by using the key name:

$red['ud_1']->set("key1", "value1");