Correction status:qualified
Teacher's comments:
以下为自己对关联数组和索引数组的遍历的理解:
<?php /** * Created by PhpStorm. * User: hongda * Date: 19/04/2018 * Time: 9:25 PM */ //关联数组的遍历 //1.for循环 $student = ['id'=>'1101','name'=>'hongda','major'=>'could computing','course'=>'Cisco']; for ($i=0; $i<count($student);$i++ ){ echo '['.key($student).']=>'.current($student).'<br>'; next($student); } echo '<hr>'; reset($student); //2.while循环 $j = 0; while ($j <count($student) ){ echo '['.key($student).']=>'.current($student).'<br>'; next($student); $j++; } echo '<hr>'; reset($student); //3.foreach() loop foreach ($student as $key => $value ){ echo '['.$key.']=>'.$value.'<br>'; } echo '<hr>'; //索引数组的遍历 $teacher = ['peter','male','php','www.php.cn',100]; //1.for循环 //对于按照循序排序的索引数组可以不用指针操作 for($x=0;$x<count($teacher);$x++){ echo '['.$x.']=>'.$teacher[$x].'<br>'; } echo '<hr>'; //对于不按顺序的关系型数组 需要用指针操作进行数组遍历 方法同上面的关系数组 //2.while循环 $y = 0; while ($y < count($teacher)){ echo '['.$y.']=>'.$teacher[$y].'<br>'; $y++; } echo '<hr>'; //3.foreach() loop foreach($teacher as $z => $z_value){ echo '['.$z.']=>'.$teacher[$z].'<br>'; } echo '<hr>'; foreach($teacher as $z => $z_value){ echo '['.$z.']=>'.$z_value.'<br>'; } ?>
点击 "运行实例" 按钮查看在线实例
可见关联数组实现遍历 必须要用到指针 而标准的索引数组则可以不用指针就可以完成遍历
以下为自己对array_splice()的函数的用法的总结的手写作业以及代码的试验
<?php /** * Created by PhpStorm. * User: hongda * Date: 19/04/2018 * Time: 10:30 PM */ //array_splice() 的用法 $color = ['red','green','blue','yellow']; print_r(array_splice($color,2)); //删除了 位置2 以及之后的元素 echo '<br>'; print_r($color); echo '<hr>'; $color = ['red','green','blue','yellow']; print_r(array_splice($color,1,-1)); //删除 位置1 到位置为 -1 //(即最后一位)之间的元素 区间为 [ ) echo '<br>'; print_r($color); echo '<hr>'; $color = array("red", "green", "blue", "yellow"); print_r(array_splice($color, 1, count($color), "orange"));//删除 //了位置为1以及1以后的所有元素 然后加上了 replacement里面的新元素 echo '<br>'; print_r($color); echo '<hr>'; $color = array("red", "green", "blue", "yellow"); print_r(array_splice($color, -1, 1, array("black", "maroon"))); //删除了yellow 加上了两个新元素 echo '<br>'; print_r($color); echo '<hr>'; $color = array("red", "green", "blue", "yellow"); print_r(array_splice($color, 3, 0, "purple")); //什么都没 删除 加上了新元素 purple echo '<br>'; print_r($color); echo '<hr>'; $a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"); $a2=array("a"=>"purple","b"=>"orange"); print_r(array_splice($a1,0,2,$a2)); //从0位置开始 两位长度被替换 echo '<br>'; print_r($a1); echo '<hr>'; //CRUD(增删改查)操作 //增 //在数组最后增加元素 $color = ['red','green','blue','yellow']; array_splice($color,count($color),0,'black'); print_r($color); echo '<br>'; //在数组最前面增加元素 $color = ['red','green','blue','yellow']; array_splice($color,0,0,'black'); print_r($color); echo '<br>'; //删 //删除数组最前面的元素 $color = ['red','green','blue','yellow']; array_splice($color,0,1); print_r($color); echo '<br>'; //删除数组最后面的元素 $color = ['red','green','blue','yellow']; array_splice($color,count($color)-1,1); print_r($color); echo '<br>'; //改 $color = ['red','green','blue','yellow']; array_splice($color,1,1,'black');//替换位置为1的元素 print_r($color); echo '<br>'; ?>
点击 "运行实例" 按钮查看在线实例
以上基本可以实现所有的增删更新 但是 查 不太明白。