您现在的位置是:主页 > php交流 >

php 中的for and foreach遍历数组

2021-05-01 20:23:36php交流 25人已围观

1. for的遍历数组

1) for in javascript

<body>
<script src=""></script>
<script type="text/javascript">
let news = [‘asia new’,’europe news’,’america news’];
for (let index = 0; index < news.length; index++) {
alert(news[index]);
}
</script>
</body>


#### 2) forEach in javascript
<body>
<h2><?echo=’News’?></h2>
<ol>
<li><a href=""<?echo 'asia news'?></a></li>
<li><a href=""<?echo 'europe news'?></a></li>
<li><a href=""<?echo 'america news'?></a></li>
</ol>
<script src=""></script>
<script type="text/javascript">
let news = [‘asia news’,’europe news’,’america news’];
news.forEach(news=>console.log(news));
</script>
</body>

3) for in php

<?php
$news = [‘asia news’,’europe news’,’america news’];
for ($i=0; $i < count($news) ; $i++) {
echo $news[$i]. ‘<br>‘;
}
?>

效果:

3) foreach in php

<?php
$news = [‘asia news’,’europe news’,’america news’];
foreach($news as $news)
{
echo $news.’<br>‘;
}
?>

效果跟上面是一样的


随机图文

站点信息

  • 文章统计 86 篇文章
  • 微信公众号:扫描二维码,关注我们