PHP foreach语句如何控制循环次数?下面程序那里不对?
<?php$state=array("one","two","three");print_r($state);echo"<br/>";array_unshift($sta...
<?php
$state = array("one","two","three");
print_r($state);
echo "<br/>";
array_unshift($state,"zero","right");
foreach($state as $states) //如何限制FOREACH的循环次数?
{
vprintf(" %s %s %s %s %s",$state);
}
echo "<br/>";
array_push($state,"four","five");
print_r($state);
?>
输出结果
Array ( [0] => one [1] => two [2] => three )
zero right one two
threezero right one two threezero right one two threezero right one two
threezero right one two three //这里我只想让输出 zero right one two three 而为什么程序循环了5次?求解
Array ( [0] => zero [1] => right [2]
=> one [3] => two [4] => three [5] => four [6] => five )
这里第二步我只想让输出 zero right one two three 而为什么程序循环输出了5次?求解 展开
$state = array("one","two","three");
print_r($state);
echo "<br/>";
array_unshift($state,"zero","right");
foreach($state as $states) //如何限制FOREACH的循环次数?
{
vprintf(" %s %s %s %s %s",$state);
}
echo "<br/>";
array_push($state,"four","five");
print_r($state);
?>
输出结果
Array ( [0] => one [1] => two [2] => three )
zero right one two
threezero right one two threezero right one two threezero right one two
threezero right one two three //这里我只想让输出 zero right one two three 而为什么程序循环了5次?求解
Array ( [0] => zero [1] => right [2]
=> one [3] => two [4] => three [5] => four [6] => five )
这里第二步我只想让输出 zero right one two three 而为什么程序循环输出了5次?求解 展开
展开全部
foreach($state as $states)
{
vprintf(" %s %s %s %s %s",$state);
}
你这个语句出问题了。请看清楚参数。你这是把$state作为参数!肯定会重复打印$state五次。
改成:
foreach($state as $states)
{
echo $states.' ';
}
就行了~
{
vprintf(" %s %s %s %s %s",$state);
}
你这个语句出问题了。请看清楚参数。你这是把$state作为参数!肯定会重复打印$state五次。
改成:
foreach($state as $states)
{
echo $states.' ';
}
就行了~
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
foreach($state as $states) //如何限制FOREACH的循环次数?
{
printf(" %s",$states);
}
{
printf(" %s",$states);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
333333333333333
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
看基础手册先吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询