开发者

Printing specific items in an array

I'm a newbie and trying to do this for my college class. I have created an array called birds: Here is my code:

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

I need to print out specific items within the array using foreach. would the next code be

Foreach($birds as $key =>value){pri开发者_如何学Cnt "$key $1,2,3 <br>";}


The array needs to be initialized with this syntax:

$birds = array(
    "Whip-poor-will", 
    "Chickadee", 
    "Pileated Woodpecker", 
    "Blue Jay", 
    "Rufus-sided Townee", 
    "Scarlet Tanager");

And the foreach would be:

foreach($birds as $key => $value)
{
  echo "$key - $value<br/>";
}

If you wanted to get data for specific elements of the array, they are referenced by index:

echo $birds[0];
//output will be: Whip-poor-will

echo $birds[2];
//output will be: Pileated Woodpecker


A bit too picky, perhaps, but it's actually <br /> (with a space). At least in XHTML standard code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜