Home >Backend Development >PHP Tutorial >How to use the method in array_map?

How to use the method in array_map?

WBOY
WBOYOriginal
2016-08-20 09:04:052407browse

array_map(function($v)use($status){

<code>   return $this->_formatProject($v,$status);</code>

},$projects)

Reply content:

array_map(function($v)use($status){

<code>   return $this->_formatProject($v,$status);</code>

},$projects)

As you can see

<code>array_map(function($v) use($status){
    return $this->_formatProject($v,$status);
},$projects);</code>

There is closure here. The method of passing parameters to closures in PHP is currently recommended user, and you can use global

in older versions.

When the closure wants to use external variables, use use.

<code class="php"><?php 
$i = 1;
$callback = function($params) {
    echo $i;    //不能这样使用$i
};

$callback = function($params) use ($i) {
    echo $i;    //正确的用法
};</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn