实例
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>PHP 中数组的遍历</title> <link rel="stylesheet" href="http://1.meleezhao.applinzi.com/storm/ross.css"> </head> <body> <header>PHP 中数组的遍历</header> <div class="wrap"> <? $indexArray= [false,1,2,'3','4','five',['one','two'],8.8]; $relevancyArray = ['id'=>5,'name'=>'ross','sex'=>'保密']; echo '<div class="one"><p>索引数组</p>'; echo '<pre>'; print_r($indexArray); echo '</pre>'; echo '</div>'; echo '<div class="one"><p>关联数组'; echo '<pre>'; print_r($relevancyArray); echo '</pre></p></div>'; echo '<div class="one"><p>for 循环输出</p><pre>'; for($i=0;$i<count($indexArray);$i++){ echo $i." : ".$indexArray[$i]."\n"; } for($i = 0;$i<count($relevancyArray);$i++){ echo $i." : ".$relevancyArray[$i]."\n"; } echo '</pre></p></div>'; //while() 输出 echo '<div class="one"><p> while() 输出索引<pre>'; $num= 0; while($num<count($indexArray)){ echo $num."=>".$indexArray[$num]."\n"; $num++; } echo '</pre></p></div>'; echo '<div class="one"><p> while() 输出关联数组<pre>'; $num= 0; while($num<count($relevancyArray)){ echo $num."=>".$relevancyArray[$num]."\n"; $num++; } echo '</pre></p></div>'; //foreach 输出 echo '<div class="one"><p> foreach 输出索引<pre>'; foreach($indexArray as $k => $v){ echo $k.'===>'.$v."\n"; } echo '</pre></p></div>'; echo '<div class="one"><p> <pre>'; foreach($relevancyArray as $k => $v){ echo $k.'===>'.$v."\n"; } echo '</pre></p></div>'; echo '<h3>array_splice() 删除 、替换、添加元素</h3>'; $deleteValue = array_splice($relevancyArray,2); echo '<p>删除后数组的值是:<pre>'; print_r($relevancyArray); echo '</pre></p>'; echo '<p>删除的值为: <pre>'; print_r($deleteValue); echo '</pre></p>'; echo '<p>添加元素: <pre>'; array_splice($relevancyArray,0,0,["one","two"]); print_r($relevancyArray); echo '</pre></p>'; echo '<p>替换元素:<pre>'; $t=time(); $s = array_splice($relevancyArray,0,2,["city"=>"重庆","date"=>$t]); print_r($relevancyArray); echo '<p>删除的值是:</p>'; print_r($s); echo '</pre></p>'; ?> </div> <footer>End..</footer> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例