批改状态:合格
老师批语:新数组的索引从0开始,最后再用array_values排列下数组索引
代码块
<?php/*** 回调函数*/$fun = function ($a,$b) {return $a+$b;};//Closure闭包function test(Closure $callback){$a = 10;$b = 20;echo $callback($a,$b);}test($fun);echo "<hr>";//单线程函数改成异步回调执行function test2(string $name):string{return 'hello'.$name;}echo call_user_func('test2','chentuan')."<hr>";echo call_user_func_array('test2',['333'])."<hr>";//回调一个全局函数//类方法的异步调用class User{public function hello(string $name):string{return 'hello'.$name;}public static function say($site){return 'welcome'.$site;}}echo call_user_func_array([new User(),'hello'],['张三'])."<hr>";echo call_user_func_array("User::say",['张三'])."<hr>";/*** 递归函数* 函数自身调用自己* 需要一定的条件判断*/function delete_dir_file($dir){$init = false;if (is_dir($dir)){//成功打开目录流if ($handle = opendir($dir)){while(($file = readdir($handle)) !== false){// echo $file."<br>";//判断子目录是否合法if ($file != '.' && $file != '..'){if (is_dir($dir.'\\'.$file)){delete_dir_file($dir.'\\'.$file);//检测到file是文件属于else} else {// unlink()删除文件unlink($dir.'\\'.$file);}}}}//关掉目录句柄closedir($handle);echo $dir;die;if (rmdir($dir)){$init = true;}}return $init;}$path = __DIR__;echo $path."<hr>";//$res = delete_dir_file($path);//if ($res) echo "目录删除成功";/*** 数组*//*** 如何将以下二维数组里的键值name换成user ,其他保持不变*/$data = [['name'=>'zhangdan','id'=>2],['name'=>'lisi','id'=>1]];$arr = [];foreach ($data as $s => $v) {$arr[$s]['user'] = $data[$s]['name'];$arr[$s]['id'] = $data[$s]['id'];}echo "<pre>";$data = $arr;print_r($data);/*** 生成一个由1-100组成的数组,要求返回该数组中的偶数组成的新数组,并且新数组的索引从0开始*///快速创建数组$arr = range(1,100);$arr = array_filter($arr,function ($v){if ($v%2==0) return $v;});print_r($arr);
效果图

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号