Accessing variable outside redis pipelining function on Laravel

973 views Asked by At

I am trying simple redis pipelining command using laravel and have an issue :

$a = array("1","2","3");
Redis::pipeline(function($pipe)
{
   for ($i = 0; $i < count($a); $i++)
   {
      $pipe->set("key:$a", $a);
   }
});

And I got 'Undefined variable: a '. I think I am missing something here. Anybody can help?

1

There are 1 answers

0
Nam Duong On BEST ANSWER

This way you can make a variable to be visible within an anonymous function's scope:

$a = array("1","2","3");
Redis::pipeline(function($pipe) use ($a)
{
   for ($i = 0; $i < count($a); $i++)
   {
      $pipe->set("key:$a", $a);
   }
});