Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP - problem printing array with 'foreach'

not sure how to use multiple values with 'foreach', an alternative?

10:37 am on Jul 17, 2001 (gmt 0)

10+ Year Member

Hi. I'm trying to get the code below to echo a list of images using an array. But I can only get it to work with one varible (the name). So I end up with width="Array" etc...

I've tried foreach ($images as $image, $widths as $width, $lefts as $left) (and retrospectivly changed $width = array... to $widths = array... etc.)
But it says it has too many parimiters, or something like that.

Should I be doing this another way? Like using 'while' (which I don't know how to use).


$images = array('00', '01', '02', '03', '04', '05', '06', '07', '08', '09');
$width = array('25', '25', '26', '26', '26', '27', '25', '27', '26', '25');
$left = array('31', '56', '81', '107', '133', '159', '186', '211', '238', '264');

foreach ($images as $image) {
echo '<a href=""><img src="pix/m_'.$image.'.gif" width="'.$width.'" height="16" id="'.$image.'" style="border: 0px; position:absolute; left:'.$left.'px; top:6px;" /></a>'."\n\n";



10:55 am on Jul 17, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

Assuming all your arrays have the same number of elements, a way of doing it would be something like:

for ($i=0; $i < $num_elements; $i++) {
echo("<img src=\"$image[$i]\" width=\"$width[$i]\" height=\"16\">");

That basically increments the variable $i which you then use as an index to the array eg $image[$i]