以下代码实现了for , while , foreach 对数组的循环遍历操作。
实例
<?php $test=['t1'=>1,'t2'=>'tt','t3'=>3,'t4'=>'ts']; for ($i=0; $i < count($test); $i++) { echo '['.key($test).'=>'.current($test).']'.'<br>'; next($test); } echo "<hr>"; reset($test); $i=0; while ($i<count($test)) { echo '['.key($test).'=>'.current($test).']'.'<br>'; next($test); $i++; } echo "<hr>"; $test=['test1'=>11,'test2'=>22,'test3'=>33,'test4'=>44]; echo '<table cellspacing="0" cellpadding="5" border="1px ">'; echo '<tr align="center" bgcolor="skyblue">'; echo "<td>t1</td><td>t2</td><td>t3</td><td>t4</td>"; echo "</tr>"; echo "<tr>"; foreach ($test as $value) { echo "<td>".$value."</td>"; } echo "</tr>"; echo "</table>"; echo "<hr>"; echo "移出的元素:"; print_r(array_splice($test,1,2,array('te'=>55))); echo "<br>插入新元素后的数组:"; print_r($test);
运行实例 »
点击 "运行实例" 按钮查看在线实例