PHP在匿名函数中继承全局变量

I can't use variable $data in Anonymous functions:

$data = array(...);
Excel::create('Filename',function($excel){
  foreach($data as $v){
     //...
  }
});

I get error: "Undefined variable: $data"

Also:

 $data = array(...);
 Excel::create('Filename',function($excel){
   global $data;
   foreach($data as $v){
      //...
   }
 });

I get error "Invalid argument supplied for foreach()"

How I can use $data in Anonymous function?

you need to use use with closure as below,

$data = array(...);
Excel::create('Filename',function($excel) use ($data){
  foreach($data as $v){
     //...
  }
});