批改状态:合格
老师批语:对海量重复数据, 进行批量处理, 这是计算机最擅长干得活, 而数组是这些数据的最佳表现形式
<?php//1.索引数组的遍历$users = [0=>1, 1=>'张三', 2=>'男', 3=>'小学学php', 4=>18];printf('<pre>%s</pre>', print_r($users, true));//重零开始递增的正整数,键名可以省略$users = [1, '张三', '男', '小学学php', 18];printf('<pre>%s</pre>', print_r($users, true));//键名可以不连续,可以是负数,还可以是小数$city = ['重庆', 3=>'北京', '天津', -3=>'上海'];printf('<pre>%s</pre>', print_r($city, true));echo '<hr>';//2.关联数组的遍历//健名使用非数字的字符串来表示//如果用数字来表示键的话,索引数组也是关联数组$users = ['id'=>'1', 'name'=>'张三', 'sex'=>'男', 'education'=>'小学学php', 'age'=>18];printf('<pre>%s</pre>', print_r($users, true));// 所以可以认为索引数组是关联数组的子集$users = [0=>1, 'name'=>'张三', 2=>'男', 'education'=>'小学学php', 18];printf('<pre>%s</pre>', print_r($users, true));// 3.数组的定义和访问//原始创建数组的方法$user = array('2', '李四', '20', '19299829@php.cn');$user1 = array('id'=>'2', 'name'=>'李四', 'age'=>'20', 'email'=>'19299829@php.cn');printf('<pre>%s</pre>', print_r($user, true));//现在定义数字建议用下面这种$user2 = ['2', '李四', '20', '19299829@php.cn'];$user3 = ['id'=>'2', 'name'=>'李四', 'age'=>'20', 'email'=>'19299829@php.cn'];printf('<pre>%s</pre>', print_r($user3, true));// 一个一个创建数组里面的成员//也叫赋值的方法来创建//关联数组$user4['id'] = '3';$user4['name'] = '王麻子';$user4['age'] = '5';$user4['email'] = '1111@php.cn';printf('<pre>%s</pre>', print_r($user4, true));echo '<hr>';//索引数组$user4[] = '3';$user4[] = '王麻子';$user4[] = '5';$user4[] = '1111@php.cn';printf('<pre>%s</pre>', print_r($user4, true));echo '<hr>';//数组成员的访问方式//索引数组echo "$user4[1]的邮箱是:{$user4[3]}<br>";//关联数组echo "{$user4['name']} 的邮箱是:{$user4['email']}";echo '<hr>';//数组成员的类型,类型不受限制$email = '1988838@php.cn';$user5 = ['id'=>'2', 'name'=>'李四', 'age'=>'20', 'email'=>$email];printf('<pre>%s</pre>', print_r($user5, true));echo '<hr>';//数组的值任然是数组的话,就创建一个多维数组,多维数组中最常用的就是二维数组//因为重数据表查询的结果,大多以二维数组的形式呈现$user6 = [['id'=>'1', 'name'=>'张三', 'age'=>'17'],['id'=>'2', 'name'=>'李四', 'age'=>'18'],['id'=>'3', 'name'=>'王麻子', 'age'=>'19'],];printf('<pre>%s</pre>', print_r($user6, true));echo '<hr>';$user7 = null;$user7['1'] = ['id'=>'1', 'name'=>'张三', 'age'=>'17'];$user7['2'] = ['id'=>'2', 'name'=>'李四', 'age'=>'18'];$user7['3'] = ['id'=>'3', 'name'=>'王麻子', 'age'=>'19'];printf('<pre>%s</pre>', print_r($user7, true));echo $user7[3]['name'];




<?php//数组的遍历//1.一个一个遍历$workForce = ['id'=>'1', 'name'=>'张三', 'age'=>'22', 'pos'=>'CEO', 'wages'=>1000];//current():当前数组成中的值。key()printf('[ %s ] => %s <br>', key($workForce), current($workForce));//用next来继续遍历next($workForce);printf('[ %s ] => %s <br>', key($workForce), current($workForce));next($workForce);printf('[ %s ] => %s <br>', key($workForce), current($workForce));next($workForce);printf('[ %s ] => %s <br>', key($workForce), current($workForce));//遍历前一条prev($workForce);printf('[ %s ] => %s <br>', key($workForce), current($workForce));//遍历最后一条end($workForce);printf('[ %s ] => %s <br>', key($workForce), current($workForce));//指针回到第一个元素reset($workForce);printf('[ %s ] => %s <br>', key($workForce), current($workForce));echo '<hr>';//2.自动遍历,是通过循环来reset($workForce);while(true){printf('[%s] =>%s<br>', key($workForce), current($workForce));if (next($workForce)) {continue;} else{break;}}echo '<hr>';//3.用foreach来遍历中间不会中断$workForces = ['id'=>'1', 'name'=>'张三', 'age'=>null, 'pos'=>'CEO', 'wages'=>1000];foreach ($workForces as $value) {printf('%s<br>',$value);}echo '<hr>';//获取数组成员的键与值foreach ($workForces as $key => $value){printf('%s<br>', $value);}echo '<hr>';foreach ($workForces as $key => $value) {printf('[%s] => %s<br>', $key, $value);}echo '<hr>';$user7 = null;$user7['1'] = ['id'=>'1', 'name'=>'张三', 'age'=>'17'];$user7['2'] = ['id'=>'2', 'name'=>'李四', 'age'=>'18'];$user7['3'] = ['id'=>'3', 'name'=>'王麻子', 'age'=>'19'];//foreach($user7 as $user7){// printf('%s', print_r($value,true));// printf('id: %s---name: %s---age:%s<br>',$user7['id'], $user7['name'], $user7['age']);//}//echo '<hr>';foreach($user7 as list('id'=>$id, 'name'=>$name, 'age'=>$age)){// printf('%s', print_r($value,true));printf('id: %s---name: %s---age:%s<br>',$id, $name, $age);}echo '<hr>';

<?php//用回调函数操作数组//1.array_filter($array,callback)将数组元素依次传入到回调函数中处理$arr = [0, 1, 2, false, '', null, "0"];$arr = array_filter($arr, function ($val) {if ($val === 0 || $val != false) {return true;} else {false;}});printf('<pre>%s</pre>', print_r($arr, true));echo '<hr>';// 2.array_walk($array,callback)//将数组中的元素用于某种操作$arr = ['张三','李四','隔壁老王'];array_walk($arr,function($val,$key){echo "{$key} 是 {$val} <br/>";});//改变数组中的值,传参的时候使用引用array_walk($arr,function(&$val,$key){$val .= $val;});printf('<pre>%s</pre>', print_r($arr, true));echo '<hr>';// 3.array_map(callback,$arr1,$arr2...)// 将回调函数作用到每个数组上,并返回一个新数组:默认索引数组// 回调函数的参数数量,必须与要处理的数组的数量一致//创建一个回调函数$arr1 = [1,2,3,4,5];$arr2 = [6,7,8,9,10];//函数写前面,数组参数写后面$new_arr = array_map(function($val1,$val2){return $val1 + $val2;},$arr1,$arr2);printf('<pre>%s</pre>', print_r($new_arr, true));

array_slice() array_splice()
<?php//array_slice()$arr = array( 'a' , 'b' , 'c' , 'd' , 'e' );$test = array_slice ( $arr , 2 );$test = array_slice ( $arr , - 2 , 1 );$test = array_slice ( $arr , 0 , 3 );print_r ( array_slice ( $arr , 2 , - 1 ));echo '<hr>';print_r ( array_slice ( $arr , 2 , - 1 , true ));echo '<hr>';//array_splice$a1=array('a'=>'男友','b'=>'老公','c'=>'闺蜜','d'=>'姐妹');$a2=array('朋友','小三');//重第一个更换2个array_splice($a1,0,2,$a2);printf('<pre>%s</pre>', print_r($a1, true));

<?php//array_chunk将一个数组分割成多个$age=array('zhu'=>'1','gou'=>'5','mao'=>'2','ji'=>'5');print_r(array_chunk($age,2,true));echo ('<hr>');//array_combine创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值$name=array('zhu','gou','mao');$age=array("1","5","6");$arr=array_combine($name,$age);printf('<pre>%s</pre>', print_r($arr, true));echo ('<hr>');//array_fill用给定的值填充数组//前面1表示重1开始后面6表示一直到6,全部用后面的值填充$test=array_fill(1,6, 'php.cn');printf('<pre>%s</pre>', print_r($test, true));echo ('<hr>');//array_flip交换数组中的键和值$test1=array('id'=>'admin','position'=>'管理员');$arr=array_flip($test1);printf('<pre>%s</pre>', print_r($arr, true));echo ('<hr>');//array_keys返回数组中所有的键名$name=array('1'=>'zhu','2'=>'gou','3'=>'mao','4'=>'ji');$arr=array_keys($name);printf('<pre>%s</pre>', print_r($arr, true));echo ('<hr>');//array_pad用值将数组填补到指定长度$name=array('1'=>'zhu','2'=>'gou','3'=>'mao','4'=>'ji');$arr=array_pad($name,8,'ya');printf('<pre>%s</pre>', print_r($arr, true));echo ('<hr>');//array_sum计算数组中所有值的和$age=array('zhu'=>'1','gou'=>'5','mao'=>'2','ji'=>'5');$arr=array_sum($age);printf('<pre>%s</pre>', print_r($arr, true));echo ('<hr>');//array_unique移除数组中重复的值$name=array('1'=>'zhu','2'=>'gou','3'=>'mao','4'=>'ji','4'=>'ji','4'=>'ji','4'=>'ji');$arr=array_unique($name);printf('<pre>%s</pre>', print_r($arr, true));echo ('<hr>');//array_multisort对多个数组或多维数组进行排序$arr1=array(1,30,15,7,25);$arr2=array(4,30,20,41,66);$num=array_merge($arr1,$arr2);array_multisort($num,SORT_DESC,SORT_NUMERIC);printf('<pre>%s</pre>', print_r($num, true));


通过本节课的学习,学到了很多数组函数以及它们的使用方法,使用数组函数操作数组比较方便
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号