The following is the array_map, array_walk and anonymous functions in php that I have compiled for you. Interested students can take a look.
<?php function showArray($cols) { foreach ($cols as $obj) { echo $obj . " "; } echo "<br>"; } function showObj($obj) { echo $obj . " "; } //mswap传入的是引用类型。 function mswap(&$a,&$b) { $tmp=$a; $a=$b; $b=$tmp; } function bubbleSort(&$cols) { $len=count($cols); for ($i=0; $i <$len ; $i++) { for ($j=1; $j < $len-$i; $j++) { if ($cols[$j-1]>$cols[$j]) { mswap($cols[$j-1],$cols[$j]); } } } } $data = array(8,2,3,9,0,45,35,235); //排序 bubbleSort($data); //打印数组 showArray($data); //使用回调函数依次遍历打印数组 array_map('showObj',$data); echo "<br>"; //使用匿名函数遍历打印数组 array_map(create_function('$obj','echo $obj." " ;'),$data); echo "<br>"; //使用新的方法(php5.3+)创建匿名函数 array_map(function($obj){echo $obj. " ";},$data); //匿名函数捕获外部变量 $msg="val:"; array_map(function($obj)use ($msg){echo $msg.$obj." ";},$data); ?>
The above is array_map, array_walk and anonymous functions in php that I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
Detailed introduction to the scope in PHP based on the code
Detailed explanation of require, include in PHP, use distinction
Detailed explanation of php class and method keyword tutorial
The above is the detailed content of I will explain to you in detail the array_map, array_walk and anonymous functions in php based on the code.. For more information, please follow other related articles on the PHP Chinese website!