unxposed Posted November 7, 2008 Share Posted November 7, 2008 Hi, I've just turned my array into a 2 dimensional array, and it works great: while ($row = mysql_fetch_assoc($result)) { $images_sub = array ( 'name' => $row['name'], 'description' => $row['description_' . $language], ); $images[] = $images_sub; } I'm now just struggling on how I'd now change my foreach loop to call the different variables: foreach ($images as $image) { echo '<img src="$image['name']" alt="$image['description']" title="$image['description']" />'; } Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/131763-solved-foreach-loop-for-2-dimensional-array/ Share on other sites More sharing options...
Adam Posted November 7, 2008 Share Posted November 7, 2008 foreach ($images as $imageArr) { foreach ($imageArr as $image) { echo '<img src="$image['name']" alt="$image['description']" title="$image['description']" />'; } } .. should do the trick! Link to comment https://forums.phpfreaks.com/topic/131763-solved-foreach-loop-for-2-dimensional-array/#findComment-684432 Share on other sites More sharing options...
Adam Posted November 7, 2008 Share Posted November 7, 2008 Oh no sorry - it's early! Not thinking right.. I would hae thought it would work with what you have? Adam Link to comment https://forums.phpfreaks.com/topic/131763-solved-foreach-loop-for-2-dimensional-array/#findComment-684433 Share on other sites More sharing options...
unxposed Posted November 7, 2008 Author Share Posted November 7, 2008 Oh god, yeah sorry it is! I'm not sure why it wasn't working for a minute there. I don't suppose there's an easy way to only get the first set of values passed through the foreach loop, or get all but the first set of values? Thanks, and sorry again. Link to comment https://forums.phpfreaks.com/topic/131763-solved-foreach-loop-for-2-dimensional-array/#findComment-684441 Share on other sites More sharing options...
unxposed Posted November 7, 2008 Author Share Posted November 7, 2008 i.e. 1. display $images[0] only in first foreach 2. display $images[1+] in second foreach (skip first values) I need this as I'm putting the first set of values within different html from the rest (first image is displayed large, rest are thumbnails). Thanks Link to comment https://forums.phpfreaks.com/topic/131763-solved-foreach-loop-for-2-dimensional-array/#findComment-684452 Share on other sites More sharing options...
Mchl Posted November 7, 2008 Share Posted November 7, 2008 Add a counter: $i = 0; foreach() { if (0 == $i) { //display large image } else { //display thumbnail } $i++; } Link to comment https://forums.phpfreaks.com/topic/131763-solved-foreach-loop-for-2-dimensional-array/#findComment-684455 Share on other sites More sharing options...
unxposed Posted November 7, 2008 Author Share Posted November 7, 2008 Of course. Oh dear, I think i've revealed how bad I am at php. Thank you. Link to comment https://forums.phpfreaks.com/topic/131763-solved-foreach-loop-for-2-dimensional-array/#findComment-684461 Share on other sites More sharing options...
Mchl Posted November 7, 2008 Share Posted November 7, 2008 Nah... Simplest solutions are often hardest to come up with. Link to comment https://forums.phpfreaks.com/topic/131763-solved-foreach-loop-for-2-dimensional-array/#findComment-684463 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.