PHP array_walk_recursive and array_map usage and differences and in which a closure (anonymous function) use ()

array_walk_recursive ()

Closure function can function with two parameters, a first value, given as a second key

$data = [];

array_walk_recursive
($value,function($v,$k) use(&$data) { $data[] = $v; });

return $data;

$ Value (array) come in an array of wear

$ V pass loop closure function value value

$ K cycle pass key values ​​closure function

use () connected to the external closure and its role is to scope variables variables inherited from the parent

& $ Data variable outer closure function passed to closure, to pass the value

----------------------------------------------------------------------------------------------------------

Another way:

$sweet = array(
  'a' => 'apple',
  'b' => 'banana'
);

$fruits = array(

'sweet' => $sweet,
  'sour' => 'lemon'
);

function test_print($item, $key) {
  echo " $key holds $item <br/>";
}

array_walk_recursive($fruits, 'test_print');

Reference Address: https://www.jb51.net/article/97402.htm

 

 

 

array_map()

Closure function value passed value only

class demo {

    public function map($arr) {
        array_map([$this,"aa"],$arr);
    }

    public function aa($v) {
       echo "<pre>";
       print_r($v);
    }

}

$arr  传入的数据

$this 调用这个class

$v 传入的数组

-----------------------------------------------------

另一种写法:

array_map(function($v) use(&$data) {

        return $data;

},$arr);

参考地址 : https://www.cnblogs.com/lbcheng/p/7813888.html

 

 

 

use()

参考地址 :https://blog.csdn.net/echojson/article/details/80633118

 

 

 

Guess you like

Origin www.cnblogs.com/http-500/p/12200037.html