Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

populating array from an array 1

Status
Not open for further replies.

craigey

Technical User
Apr 18, 2002
510
GB
Hi,

I'm listing a directory & trying to use a foreach loop to empty the list into an array, but keep running into problems "Parse error: syntax error, unexpected T_FOREACH, expecting ')' ".
Can anyone shed some light, please?

Code:
$timestorepeat = 5;

//path to directory to scan
$scrolldirectory = "includes/images/Right-Hand-Scroll/";
 
//get all image files with a .jpg extension.
$scrollimages = glob("" . $scrolldirectory . "*.jpg");

$i = 0;
	
$items = array(
foreach ($scrollimages as $image){	
++i;
$i => array(
			'href' => ''.$image.'',
			'title' => ''.$image.'',
			'src' => ''.$image.'',
			'alt' => ''.$image.'',
			'border' => '0',
			'width' => '110',
			'height' => '80'),
}
);

//shuffle the master array randomly
shuffle($items);

$count = 0;

while ($count <=$timestorepeat)
{
//now output them
foreach ($items as $item) {

    echo <<<HTML
    <tr>
        <td class="infoBoxContents" valign="top"></td>
        <td class="infoBoxContents"><center>
                <a href="{$item['href']}"
                    title="{$item['title']}" target="_new">
                    <img src="{$item['src']}"
                            alt="{$item['alt']}"
			    border="{$item['border']}"
                            width="{$item['width']}"
                            height="{$item['height']}">
				<br><br><br><br></center></td></tr>

HTML;
}

$count++;
}
 
Hi

The only language where such code works is Ruby.
Code:
[navy]$items[/navy] [teal]=[/teal] [b]array[/b][teal]();[/teal]

[b]foreach[/b] [teal]([/teal][navy]$scrollimages[/navy] [b]as[/b] [navy]$image[/navy][teal])[/teal] [teal]{[/teal]
  [navy]$items[/navy][teal][++[/teal][navy]$i[/navy][teal]]=[/teal][b]array[/b][teal]([/teal]
    [green][i]'href'[/i][/green] [teal]=>[/teal] [navy]$image[/navy][teal],[/teal]
    [green][i]'title'[/i][/green] [teal]=>[/teal] [navy]$image[/navy][teal],[/teal]
    [green][i]'src'[/i][/green] [teal]=>[/teal] [navy]$image[/navy][teal],[/teal]
    [green][i]'alt'[/i][/green] [teal]=>[/teal] [navy]$image[/navy][teal],[/teal]
    [green][i]'border'[/i][/green] [teal]=>[/teal] [purple]0[/purple][teal],[/teal]
    [green][i]'width'[/i][/green] [teal]=>[/teal] [purple]110[/purple][teal],[/teal]
    [green][i]'height'[/i][/green] [teal]=>[/teal] [purple]80[/purple]
  [teal]);[/teal]
[teal]}[/teal]

Feherke.
 
Perfect, Thanks.
I knew the $items = array( wasn't right, but just couldn't think how to open the array.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top