Blogger Information
Blog 18
fans 1
comment 0
visits 12267
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js 中 for,foreach 遍历数组,与PHP 中 for,foreach 遍历数组的区别----0425
木樨
Original
630 people have browsed it

js 遍历数组

  1. // js中变量需要先声明才能使用
  2. let fruits = ['apple','banana','orange'];
  3. // 1. js for 遍历数组
  4. for (let index = 0; index < fruits.length; index++) {
  5. alert(fruits[index]);
  6. }
  7. // 2. js forEach
  8. fruits.forEach(fruit=>console.log(fruit));

php 遍历数组

  1. //php变量不需要声明可以直接使用
  2. $fruits = ['apple','banana','orange'];
  3. // 1. for 遍历数组
  4. for ($i=0; $i < count($fruits) ; $i++) {
  5. echo $fruits[$i].'<br>'; //"."为 php 的连接符
  6. }
  7. // 2. foreach遍历数组
  8. foreach($fruits as $k =>$fruit){
  9. echo $k. '=>' .$fruit.'<br>';
  10. }
Correcting teacher:灭绝师太灭绝师太

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post