Jump to content

[SOLVED] Nested foreach loop with two arrays


damianjames

Recommended Posts

I'm starting a new topic because I figured out my previous issue, now onto my next challenge!

 

I have an array from a session, and an array from post.  I'm trying nested foreach loops, but I can't seem to get it correct.

 

The session array ($inputname) is storing a string that I am identifying the previous screen's text input with (name parameter), and the post is the value of the text input ($link).  $inputname is also the name of each image I want to display minus the file extension.

 

I want to cycle through each and use $link as the source for an anchor tag, and use $inputname to identify the image to display. What I initially tried was:

 

foreach($inputname as $value) {
     foreach($link as $value1) {
          echo "<tr><td><a href=\"" . $value1 . "\"><img src=\"" . $value . ".jpg\" /></a></td></tr>";
     }
}

 

I also tried using a for loop within the outer foreach and I haven't gotten that to work either.

 

I've tried explode/implode/extract/pray_for_mercy on one or both of them to see if I can somehow split one into separate variables, but nothing has given me what I need.  I hope someone has an idea on what to do!  Thanks in advance for the assistance.

Hm, I can't think up what your array structure looks like so I took a stab:

 

$count = count($inputname);
for($i = 0; $i < $count; $i++){
echo "<tr><td><a href='{$link[$i]}'><img src='{$inputname[$i]}.jpg' /></a></td></tr>";
}

 

Made a lot of assumptions there. :)

Thank you very much for the help!  I'm going to go try that out... in the meantime, sorry for not showing the arrays - here they are.

 

inputname array:
Array
(
    [0] => V1_1
    [1] => V1_2
    [2] => V1_3
    [3] => V1_4
    [4] => V1_5
)

link array:
Array
(
    [V1_1] => http://1.com
    [V1_2] => http://2.com
    [V1_3] => http://3.com
    [V1_4] => http://4.com
    [V1_5] => http://5.com
)

 

Edit: Err there's a problem, now isn't there... $link is not an indexed array.

Yeah, but its not a major problem. Try this instead:

 

$count = count($inputname);
for($i = 0; $i < $count; $i++){
echo "<tr><td><a href='{$link[$inputname[$i]]}'><img src='{$inputname[$i]}.jpg' /></a></td></tr>";
}

umm... if the inputname array values are the same as the link array keys...why even bother with the inputname array at all?

 

foreach ($link as $key => $val) {
    echo "<tr><td><a href='$val'><img src='{$key}.jpg' /></a></td></tr>";
}

 

edited to add the .jpg

umm... if the inputname array values are the same as the link array keys...why even bother with the inputname array at all?

 

foreach ($link as $key => $val) {
    echo "<tr><td><a href='$val'><img src='{$key}.jpg' /></a></td></tr>";
}

 

edited to add the .jpg

 

That'd be my inexperience rearing its ugly head!  Thanks again to both of you.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.