2010-09-18 30 views
1

我是一个新手,并试图为我的大学班做到这一点。 我已经创建了一个名为数组鸟: 这里是我的代码:打印一个数组中的特定项目

<?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>";} 

回答

4

数组需要用这种语法进行初始化:

$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 
0

有点太挑剔,或许,但它实际上是<br />(有空间)。至少在XHTML标准代码中。

+0

感谢大家的帮助!我完成了一个,并学习其他新东西。多维数组!听起来很有趣:-)也许LOL – Karen 2010-09-27 13:23:43