批改状态:合格
老师批语:
<?php// *数组指针的向前prev()与向后next()// *一个名为三国的数组$sanguo=['id'=>1,'name'=>'关羽','nickname'=>'关公','tribe'=>'蜀国'];printf('[%s]=>%s<br>',key($sanguo),current($sanguo));next($sanguo);printf('[%s]=>%s<br>',key($sanguo),current($sanguo));next($sanguo);printf('[%s]=>%s<br>',key($sanguo),current($sanguo));next($sanguo);printf('[%s]=>%s<br>',key($sanguo),current($sanguo));prev($sanguo);printf('[%s]=>%s<br>',key($sanguo),current($sanguo));reset($sanguo);printf('[%s]=>%s<br>',key($sanguo),current($sanguo));end($sanguo);printf('[%s]=>%s<br>',key($sanguo),current($sanguo));

<?php// *数组的循环遍历及数组的解构// *1.用while循环遍历数组$suihu=['id'=>2,'name'=>'宋江','nickname'=>'及时雨','tribe'=>'水泊梁山'];reset($suihu);if(count($suihu)>0){while(true){printf('[%s]=>%s<br>',key($suihu),current($suihu));if(next($suihu)){continue;}else{break;}}}else{echo '空数组';}// *2.使用foreach数组循环遍历语法糖:foreach($suihu as $key=>$value){printf('[%s]=>%s<br>',$key,$value);}echo '<hr>';// *数组解构:就是把数组解析到变量中// *索引数组的解构list($name,$nickname)=['关羽','关公'];echo $name,',',$nickname,'<br>';echo '<hr>';// *关联数组的解构list('name'=>$name,'nickname'=>$nickname)=['name'=>'林冲','nickname'=>'豹子头'];echo $name,',',$nickname,'<br>';echo '<hr>';// *通过内置的extract解析关联数组extract(['name'=>'李逵','nickname'=>'黑旋风']);echo $name,',',$nickname,'<br>';

<?php// *foreach数据表以及推荐使用的解构方法// *1.使用foreach遍历数据库中的数据表$users=[['id'=>1,'name'=>'刘备'],['id'=>2,'name'=>'关羽'],['id'=>3,'name'=>'张飞'],];foreach($users as $user){vprintf('id=%s,name=%s<br>',$user);}echo '<hr>';// *2.把$users数组当作一个模板,直接将它解构,其效果也是一样的(推荐)foreach($users as list('id'=>$id,'name'=>$name)){printf('id=%s,name=%s<br>',$id,$name);}

<?php// *数组的值组成新数组及键号重置以及元素的查询//* 1.array_values():将数组的值组成新数组及键号重置$arr=[0=>1,3=>'水浒',4=>5,7=>['刘备','关羽','张飞']];printf('<pre>%s</pre>',print_r($arr,true));printf('<pre>%s</pre>',print_r(array_values($arr),true));// *2.in_array():查询某个元素是否在数组中echo(in_array('水浒',$arr)?'在数组中':'不在数组中').'<br>';echo(in_array('三国',$arr)?'在数组中':'不在数组中').'<br>';// *3.array_search():不但找到该元素,还知道该元素的索引是多少,从而也知道该元素的值$key=array_search('水浒',$arr);echo $key,'<br>',$arr[$key];

<?php// *数组元素的值的展示与计算// *1.array_unique():去掉数组重复的值$lgz=[0=>'刘备',1=>'关羽',2=>'张飞',3=>'刘备'];printf('<pre>%s</pre>',print_r($lgz,true));print_r(array_unique($lgz));echo '<br>';// *2.count()统计当前数组元素的数量:echo count($lgz);// *3.array_count_values():统计数组中元素的值出现的次数printf('<pre>%s</pre>',print_r(array_count_values($lgz),true));// *4.array_sum():计算数组中元素的值之和$arr=[0=>1,2=>40,3=>'10php',4=>'js10',5=>'css'];echo array_sum($arr).'<br>';// *5.array_product():计算数组中元素的值的乘积echo array_product($arr);

<?php// *数组元素的增加与删除相当于堆栈的压入与弹出// *空数组$lgz=[];// *1.array_push():数组元素压入堆栈echo array_push($lgz,'刘备','关羽','张飞').'<br>';print_r($lgz);echo '<br>';// *2.array_pop():数组的元素弹出堆栈(后进先出)echo array_pop($lgz).'<br>';echo array_pop($lgz).'<br>';print_r($lgz);echo '<br>';$sanguo=[];// *3.array_unshift():数组元素压入堆栈的另一种形式echo array_unshift($sanguo,'魏国','蜀国','吴国').'<br>';print_r($sanguo);echo '<br>';// *4.array_shift():数组元素弹出堆栈的另一种形式(先进先出)echo array_shift($sanguo).'<br>';echo array_shift($sanguo).'<br>';print_r($sanguo);echo '<br>';// *5.array_push()入,array_shift()出$student=[];echo array_push($student,'张三','李四','王五').'<br>';print_r($student);echo '<br>';echo array_shift($student).'<br>';echo array_shift($student).'<br>';print_r($student);echo '<br>';// *6.array_unshift()入,array_pop()出$product=[];echo array_unshift($product,'电脑','空调','音响').'<br>';echo array_pop($product).'<br>';echo array_pop($product).'<br>';print_r($product);

<?php// *sort():数组元素的升序(默认)$arr=[2,34,55,6,'php','welcome',7];printf('<pre>%s</pre>',print_r($arr,true));sort($arr);printf('<pre>%s</pre>',print_r($arr,true));echo '<hr>';// *asort():在前面加a则顺序时键号不会被重置$arr2=[21,134,55,62,'李四','abc',17];printf('<pre>%s</pre>',print_r($arr2,true));asort($arr2);printf('<pre>%s</pre>',print_r($arr2,true));echo '<hr>';// *rsort():数组元素的降序排列$arr3=[11,34,50,2,'a2','a',12];printf('<pre>%s</pre>',print_r($arr3,true));rsort($arr3);printf('<pre>%s</pre>',print_r($arr3,true));echo '<hr>';// *arsort():在前面加a则降序时键号不会被重置$arr4=[2,13,5,6,'js','张三',30];printf('<pre>%s</pre>',print_r($arr4,true));arsort($arr4);printf('<pre>%s</pre>',print_r($arr4,true));echo '<hr>';// *ksort():键排序(升序)$arr5=[2,34,55,6,'php','welcome',7];printf('<pre>%s</pre>',print_r($arr5,true));ksort($arr5);printf('<pre>%s</pre>',print_r($arr5,true));echo '<hr>';// *krsort():键的降序$arr6=[2,34,55,6,'php','welcome',7];printf('<pre>%s</pre>',print_r($arr6,true));krsort($arr6);printf('<pre>%s</pre>',print_r($arr6,true));echo '<hr>';// *shuffle():乱序(即每刷新一次就大乱顺序一次,可用于抽奖等场合)$arr7=[2,34,55,6,'php','welcome',7];printf('<pre>%s</pre>',print_r($arr7,true));shuffle($arr7);printf('<pre>%s</pre>',print_r($arr7,true));echo '<hr>';







<?php// *展示数组中的部分元素与删除数组中的部分元素// *1.array_slice():只展示数组的部分元素$sanguo=['id'=>1,'name'=>'关羽','nickname'=>'关公','arms'=>'青龙偃月刀'];$res=array_slice($sanguo,1,2);printf('<pre>%s</pre>',print_r($res,true));$res=array_slice($sanguo,1);printf('<pre>%s</pre>',print_r($res,true));echo '<hr>';// *2.array_splice():删除数组中的部分元素$arr=[2,34,55,6,'php','welcome',7];printf('<pre>%s</pre>',print_r($arr,true));$res1=array_splice($arr,1,2);printf('<pre>%s</pre>',print_r($res1,true));printf('<pre>%s</pre>',print_r($arr,true));

<?php// *php数组元素的替换与追加// *1.使用array_splice()功能实现替换$arr=[2,34,55,6,'php','welcome',7];printf('原始数组:<pre>%s</pre>',print_r($arr,true));$res=array_splice($arr,4,2,['js','hello'],);printf('<pre>%s</pre>',print_r($res,true));printf('新数组:<pre>%s</pre>',print_r($arr,true));echo '<hr>';// *2.使用array_splice()功能实现追加$arr1=[2,34,55,6,'php','welcome',6];printf('原始数组:<pre>%s</pre>',print_r($arr1,true));$res1=array_splice($arr1,1,0,[22,33],);// printf('<pre>%s</pre>',print_r($res1,true));printf('新数组:<pre>%s</pre>',print_r($arr1,true));


<?php// *数组过滤器// *1.留下true的元素,其余过滤掉$arr=[234,'js',true,[1,2,3],(new class(){}),[],null,false,'',0,'0'];$res=array_filter($arr);printf('<pre>%s</pre>',print_r($res,true));echo '<hr>';// *2.留下标量(数据类型),其余过滤掉$res1=array_filter($arr,function($item){return is_scalar($item);});printf('<pre>%s</pre>',print_r($res1,true));// *再次过滤printf('<pre>%s</pre>',print_r(array_filter($res1),true));

<?php// *数组拉平与迭代// *1.array_map(function($item){},$arr):把数组$arr中的元素$item拉平成字符串$arr = ['水浒',[3, 4, 5],(new class () {public string $name = '宋江';public string $nickname = '及时雨';}),15,20];// *拉平$res=array_map(function($item){switch(gettype($item)){case 'array':$item=implode(',',$item);break;case 'object':$item=implode(',',get_object_vars($item));break;}return $item;},$arr);printf('<pre>%s</pre>',print_r($res,true));echo '<hr>';// *2.array_sum():数组的值求和也是迭代的一种$arr1=[1,2,3,4,5];echo array_sum($arr1),'<br>';echo '<hr>';// *3.array_reduce():迭代:就是把这些数组元素用同一个值返回,// *或者把许多标签组合在一起,形成一个比较大的元素$res1=array_reduce($arr1,function($acc,$cur){echo $acc,',',$cur,'<br>';return $acc+$cur;},0);echo $res1,'<br>';echo '<hr>';// *4.array_walk():对模板进行一些控制$arr2=['id'=>5,'name'=>'李逵','nickname'=>'黑旋风'];array_walk($arr2,function($value,$key,$color){printf('[%s]=><span style="color:%s">%s</span><br>',$key,$color,$value);},'red');


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