回调实例
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> </body> </html> <?php /** * 数组的回调 * 参数有参数 */ //1.array_filter($arr,$callback) //2.array_walk($arr,$callback()) //1.array_filter():回调处理数组中的每个元素,仅返回结果为true的元素 $ct = [1,2,0,'','',null,false,88,false,'php',null]; echo '原数组',var_export($ct,true),'共有:',count($ct),'个元素<hr>'; $ct1 = array_filter($ct); echo '新数组',var_export($ct1,true),'共有:',count($ct1),'个元素<hr>'; //传入一个回调:匿名函数 $hs =['php','js','h5','java','python']; $hs1 = array_filter($hs, function ($sc){ return $sc !== 'python'; }); echo var_export($hs1),'<hr>'; //2.array_walk():对数组每个元素的键和值进行处理 $arr =['name'=>'root','password=>admin']; echo var_export($arr,true),'<hr>'; //格式化 array_walk($arr,function ($value,$key){ echo $key,':',$value,'<br>'; }); echo '<hr>';
运行实例 »
点击 "运行实例" 按钮查看在线实例