views:

36

answers:

3

I have an array like this:

[0] => Array
    (
        [slideritem] => 592
        [sliderbig] => 644
    )

[1] => Array
    (
        [slideritem] => 593
        [sliderbig] => 645
    )

[2] => Array
    (
        [slideritem] => 594
        [sliderbig] => 646
    )

slideritem is the id of an image that will be displayed and the slidebig is the image that will be linked to be displayed on a lightbox.

In other words i want the markup to be:

<a href="[sliderbig]"><img src="[slideritem]" /></a>

I pretty sure it's a simple foreach statement but i'm already on my 12th hour straight in front of the screen :)

A: 

Something like this?

foreach($theArray as $slider) {
    printf('<a href="%d.png"><img src="%d.png" alt="sliderimage"/></a>', 
            $slider['sliderbig'],
            $slider['slideritem']);
}

Take a break!

Gordon
Do you mean `printf()`?
Bill Karwin
thanks @Bill! Of course.
Gordon
yeap. that will do. heh, amazing. have a good weekend ppl and thx :)
tsiger
+1  A: 
foreach($array as $item) {
    echo "<a href="{$item->sliderbig}"><img src="{$item->slideritem}" /></a>";
}
Macha
You need to escape the inner quotes if you want to use the {notation} but it will also raise "Trying to get Property of Non-Object" since the $item is an array.
Gordon
A: 
function test($accum, $a) {
    return $accum . sprintf('<a href="url/to/%s">' .
        '<img src="url/to/%s" /></a>'."\n",
        htmlspecialchars($a['sliderbig']),
        htmlspecialchars($a['sliderbigitem']));
}

$output = array_reduce($array, 'test', '');
Artefacto