Accessing variable outside redis pipelining function on Laravel

喜夏-厌秋 提交于 2019-12-24 11:24:05

问题


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:


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


来源:https://stackoverflow.com/questions/27613359/accessing-variable-outside-redis-pipelining-function-on-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!