我是一个新手,正在努力为我的大学班级做这件事。我已经创建了一个名为bird的数组:以下是我的代码:
<?php // This script creates an array called birds and then prints out the birds 1-3 and then prints the birds 0,, 2 and 4
$birds = array ("Whip-poor-will|Chickadee|Pileated Woodpecker|Blue Jay|Rufus-sided Towhee|Scarlet Tanager");我需要使用foreach打印出数组中的特定项。下一个代码会是
Foreach($birds as $key =>value){print "$key $1,2,3 <br>";}发布于 2010-09-19 00:17:56
数组需要使用以下语法进行初始化:
$birds = array(
"Whip-poor-will",
"Chickadee",
"Pileated Woodpecker",
"Blue Jay",
"Rufus-sided Townee",
"Scarlet Tanager");而foreach将是:
foreach($birds as $key => $value)
{
echo "$key - $value<br/>";
}如果想要获取数组中特定元素的数据,可以通过索引来引用它们:
echo $birds[0];
//output will be: Whip-poor-will
echo $birds[2];
//output will be: Pileated Woodpecker发布于 2010-09-19 05:51:18
可能有点太挑剔了,但它实际上是<br /> (带空格)。至少在XHTML标准代码中是这样。
https://stackoverflow.com/questions/3742361
复制相似问题