2010-09-18 56 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