Search code examples
laravelredispipelining

Accessing variable outside redis pipelining function on Laravel


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?


Solution

  • 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);
       }
    });