tags:

views:

79

answers:

2

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){print "$key $1,2,3 <br>";}
+1  A: 

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
Rookz
good answer. I formatted the array for readability.
stereofrog
A: 

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

matsolof
Thanks everyone for your help! I finished that one and on to learning other new stuff. Multi dimensional arrays! sounds like fun :-) well maybe LOL
Karen