damianjames Posted November 22, 2008 Share Posted November 22, 2008 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. Link to comment https://forums.phpfreaks.com/topic/133749-solved-nested-foreach-loop-with-two-arrays/ Share on other sites More sharing options...
JasonLewis Posted November 22, 2008 Share Posted November 22, 2008 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. Link to comment https://forums.phpfreaks.com/topic/133749-solved-nested-foreach-loop-with-two-arrays/#findComment-696027 Share on other sites More sharing options...
damianjames Posted November 22, 2008 Author Share Posted November 22, 2008 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. Link to comment https://forums.phpfreaks.com/topic/133749-solved-nested-foreach-loop-with-two-arrays/#findComment-696029 Share on other sites More sharing options...
JasonLewis Posted November 22, 2008 Share Posted November 22, 2008 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>"; } Link to comment https://forums.phpfreaks.com/topic/133749-solved-nested-foreach-loop-with-two-arrays/#findComment-696032 Share on other sites More sharing options...
damianjames Posted November 22, 2008 Author Share Posted November 22, 2008 You sir - rock, and if I had a cannon I'd salute you. Thanks a ton! Link to comment https://forums.phpfreaks.com/topic/133749-solved-nested-foreach-loop-with-two-arrays/#findComment-696034 Share on other sites More sharing options...
.josh Posted November 22, 2008 Share Posted November 22, 2008 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 Link to comment https://forums.phpfreaks.com/topic/133749-solved-nested-foreach-loop-with-two-arrays/#findComment-696036 Share on other sites More sharing options...
damianjames Posted November 22, 2008 Author Share Posted November 22, 2008 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. Link to comment https://forums.phpfreaks.com/topic/133749-solved-nested-foreach-loop-with-two-arrays/#findComment-696041 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.