差一点没有完成!汗,不熟练,唯有苦学!
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>for、while、foreach遍历方法</title> </head> <body> <h2>索引数组0到10的累加和计算方法</h2> <button type="">for方法求和</button> <button type="">while方法求和</button> <button type="">foreach方法求和</button> <hr color="red"> <h2>关联数组的遍历方法</h2> <button type="">for方法遍历</button> <button type="">while方法遍历</button> <button type="">foreach方法遍历</button> <?php $sum=0; $a=array(); for($i=0;$i<=10;$i++) { array_push($a,$i); $sum+=$a[$i]; } $total=0; $b=array(); $j=0; while($j<=10) { array_push($b,$j); $total+=$b[$j]; $j++; } $totl=0; $c=array("0","1","2","3","4","5","6","7","8","9","10"); foreach ($c as $value) { $totl+=$value; } echo'<hr color="red">'; $e=''; $d=array("no1"=>"VOLVO","no2"=>"BENZ","no3"=>"BMW"); for($i=1;$i<=count($d);$i++){ $e.=key($d).'=>'.current($d).' '; next($d); } $m=''; $h=0; reset($d); $d=array("no1"=>"VOLVO","no2"=>"BENZ","no3"=>"BMW"); while($h<=count($d)){ $m.=key($d).'=>'.current($d).' '; next($d); $h++; } $n=''; foreach ($d as $key => $value) { $n.=$key .'=>'. $value.' '; } ?> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript"> $('button').eq(0).click(function(){ alert("<?php echo('for方法求和:'.$sum); ?>"); }) $('button').eq(1).click(function(){ alert("<?php echo('while方法求和:'.$total); ?>"); }) $('button').eq(2).click(function(){ alert("<?php echo('foreach方法求和:'.$totl); ?>"); }) $('button').eq(3).click(function(){ alert("<?php echo('for方法遍历:'.$e); ?>");}) $('button').eq(4).click(function(){ alert("<?php echo('while方法遍历:'.$m); ?>"); }) $('button').eq(5).click(function(){ alert("<?php echo('foreach方法求和:'.$n); ?>"); }) </script> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例