Karen
Karen

Reputation: 11

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

Upvotes: 1

Views: 6848

Answers (2)

Rookz
Rookz

Reputation: 146

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

Upvotes: 4

matsolof
matsolof

Reputation: 2715

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

Upvotes: 0

Related Questions