Fuxi
Fuxi

Reputation: 7599

php: unexpected behaviour iterating through array

i'm trying to iterate through an array as follows:

$data = array(
    "i0" => "item 0",
    "i1" => "item 1",
    "i2" => "item 2",
    "i3" => "item 3",
    "i4" => "item 4",
    "i5" => "item 5"
);


foreach($data as $id=>$capt);
{
    echo $id.": ".$capt."<br>";
}

i'm expecting getting 6 elements, but the foreach loop will only output the last item. any ideas why? thanks

Upvotes: 0

Views: 16

Answers (1)

mike.k
mike.k

Reputation: 3457

The error is in foreach($data as $id=>$capt);, it should not have a ; at the end. Remove it and the loop will work.

Upvotes: 2

Related Questions