批改状态:合格
老师批语:这几个是难以理解, 要画图
$arr = ['js' , 'css' ,25 ,'php',30];printf('<pre>%s</pre>',print_r($arr ,true));//随机去2个元素$res = array_rand($arr,2);printf('<pre>%s</pre>',print_r($res,true));

//如果数组1 和 数组2/多个数组中,出现同一个键,那么数组1的值将被数组2/最后出现的数组替换。$arr = ['id'=>201, 'name'=>'皮皮虾', 'age'=>20];$arr1 = [ 'id' => 301];//如果一个键存在于数组2/后面数组中,但是不存在数组1,则会在数组1中创建这个元素。$arr2 = [ 'id'=> 401,'QQ'=>'258'];$res = array_replace($arr, $arr1,$arr2);printf('<pre>%s</pre>',print_r($arr,true));printf('<pre>%s</pre>',print_r($res,true));

$arr = [1,10,'php','css',25];$arr1 = [8,'js',25,'php'];$res = array_intersect($arr ,$arr1);printf('<pre>%s</pre>',print_r($res,true));

$arr1 = ['id' => 102, 'name' => '苹果', 'money' => 100, '橘子'];$arr2 = ['id' => 103, 'name' => '香蕉', 'money' => 100, '西瓜','橘子'];printf('<pre>%s</pre><hr>',print_r ($arr1,true));printf('<pre>%s</pre><hr>',print_r ($arr2,true));$res = array_intersect_assoc($arr1, $arr2);printf('<pre>%s</pre>',print_r($res,true));

$arr1 = ['id' => 102, 'name' => '苹果', 'money' => 100, '橘子','西瓜'];$arr2 = ['id' => 103, 'name' => '香蕉', 'money' => 100, '西瓜'];printf('数组1:<pre>%s</pre><hr>',print_r ($arr1,true));printf('数组2:<pre>%s</pre><hr>',print_r ($arr2,true));$res = array_diff($arr1, $arr2);printf('数组1和2的差集:<pre>%s</pre>',print_r($res,true));

$arr1 = ['id' => 102, 'name' => '苹果', 'money' => 100, '橘子','西瓜'];$arr2 = ['id' => 103, 'name' => '香蕉', 'money' => 100, '西瓜'];printf('数组1:<pre>%s</pre><hr>',print_r ($arr1,true));printf('数组2:<pre>%s</pre><hr>',print_r ($arr2,true));$res = array_diff_assoc($arr1, $arr2);printf('数组1和2的差集:<pre>%s</pre>',print_r($res,true));

$input_array = array('a', 'b', 'c', 'd', 'e');$res = array_chunk($input_array, 2);printf('<pre>%s</pre>',print_r($res,true));echo '<hr>';$res = array_chunk($input_array, 2,true);printf('<pre>%s</pre>',print_r($res,true));

$arr = ['id'=> 102, 'name'=>'小猪','like'=>null];echo array_key_exists('name',$arr)? 'true' : 'false'; //返回trueecho '<hr>';// isset() 对于数组中为 NULL 的值不会返回 TRUE,而 array_key_exists() 会。echo isset($arr['like'])? 'true' : 'false'; //返回 falseecho '<hr>';echo array_key_exists('like',$arr)? 'true' : 'false'; //返回 true
$arr = [ 102, 'XiaoZhu','摄影','100'];echo in_array(102,$arr)?'true' : 'false','<br>'; //返回true// 如果第三个参数为true 则 还会检查类型是否相同。echo in_array(100,$arr)?'true' : 'false','<br>'; //返回trueecho in_array(100,$arr,true)?'true' : 'false','<br>'; // 返回false//字符串,区分大小写echo in_array('xiaoZhu',$arr)?'true' : 'false','<br>'; //返回false
$arr = ['blue', 'red','green', 'red'];printf('<pre>%s</pre>',print_r($arr,true));// 搜索的值是字符串,则比较以区分大小写的方式进行。echo $res = array_search('green', $arr),'<br>'; //返回 2// 搜索的值出现多次,则返回第一个匹配的键。echo $res = array_search('red', $arr); //返回1
$arr=[5,8,10,15,14,11,6,24];$res= array_filter($arr, function ($var){//获取数组中的偶数返回return $var=(!($var%2) );});printf('<pre>%s</pre>',print_r( $res,true));

$arr = ['星期一','星期二','星期三','星期四','星期五','星期六','星期天'];$arr1 = ['上班','上班','上班','休息','上班','休息','上班'];// shuffle()随机打乱数组顺序$res1 = shuffle($arr1);$arr=array_map(function ($a,$b){return '<font>'.'今天是:'.$a.'要'.$b.'了'.'</font>';},$arr,$arr1);printf('<pre>%s</pre>',print_r($arr,true));

$arr =[101,'张三',15,'zs@163.cn','134***','摄影'];//起始从索引2开始printf('<pre>%s</pre>',print_r(array_slice($arr, 2),true));

$arr =[101,'张三',15,'zs@163.cn','134***','摄影'];//起始从索引2开始,截止到索引-2结束(不包括索引-2的数据)printf('<pre>%s</pre>',print_r(array_slice($arr, 2,-2),true));

$arr =[101,'张三',15,'zs@163.cn','134***','摄影'];printf('原数组元素:<pre>%s</pre><hr>',print_r($arr, true));//删除元素$res= array_splice($arr, 3,2);printf('删除的元素:<pre>%s</pre>',print_r($res,true));

//替换元素$res= array_splice($arr, 3,2,['zs@qq.cn','158***']);printf('替换的元素:<pre>%s</pre>',print_r($arr,true));// $res= array_splice($arr, -4,-2,['zs@qq.cn']);// printf('替换的元素:<pre>%s</pre>',print_r($arr,true));// 增加元素$res= array_splice($arr, 1,0,['php','js']);printf('增加的元素:<pre>%s</pre>',print_r($arr,true));

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