JSHINER Posted May 2, 2007 Share Posted May 2, 2007 I am trying this: foreach ($ARR1 as $z); ($ARR2 as $y) { if($y == $z['Short']) { echo $z['Long'];} } But it is not working. How can I get two arrays in that foreach? Link to comment https://forums.phpfreaks.com/topic/49598-solved-how-to-have-two-arrays-in-a-foreach/ Share on other sites More sharing options...
clown[NOR] Posted May 2, 2007 Share Posted May 2, 2007 maybe (not sure, but try it) foreach ($ARR1 as $z && $ARR2 as $y) Link to comment https://forums.phpfreaks.com/topic/49598-solved-how-to-have-two-arrays-in-a-foreach/#findComment-243189 Share on other sites More sharing options...
JSHINER Posted May 2, 2007 Author Share Posted May 2, 2007 Figured out the solution in part: foreach ($page['references_amenities'] as $z) foreach ($APPLIANCES as $y) { if($y == $z['Short']) { echo $z['Long'], ', ';} } However that displays: "Text, Text," - How can I get that LAST comma off? Link to comment https://forums.phpfreaks.com/topic/49598-solved-how-to-have-two-arrays-in-a-foreach/#findComment-243190 Share on other sites More sharing options...
sasa Posted May 2, 2007 Share Posted May 2, 2007 try $sep=''; foreach ($page['references_amenities'] as $z) foreach ($APPLIANCES as $y) { if($y == $z['Short']) { echo $sep,$z['Long'], ', ';} $sep=', '; Link to comment https://forums.phpfreaks.com/topic/49598-solved-how-to-have-two-arrays-in-a-foreach/#findComment-243242 Share on other sites More sharing options...
JSHINER Posted May 2, 2007 Author Share Posted May 2, 2007 That now displays "Text, , Text," Link to comment https://forums.phpfreaks.com/topic/49598-solved-how-to-have-two-arrays-in-a-foreach/#findComment-243244 Share on other sites More sharing options...
boo_lolly Posted May 2, 2007 Share Posted May 2, 2007 i think this is what you're going for: <?php foreach($page['references_amenities'] as $z){ foreach($APPLIANCES as $y){ echo (($y == $z['Short']) ? ($z['Long']) : ()); } } ?> Link to comment https://forums.phpfreaks.com/topic/49598-solved-how-to-have-two-arrays-in-a-foreach/#findComment-243255 Share on other sites More sharing options...
JSHINER Posted May 2, 2007 Author Share Posted May 2, 2007 That is giving me an unexpected ')' on the echo line. Link to comment https://forums.phpfreaks.com/topic/49598-solved-how-to-have-two-arrays-in-a-foreach/#findComment-243257 Share on other sites More sharing options...
Agum Posted May 2, 2007 Share Posted May 2, 2007 actually sasa is almost right, just need a slight change... try this $sep=''; foreach ($page['references_amenities'] as $z) foreach ($APPLIANCES as $y) { if($y == $z['Short']) { echo $sep,$z['Long'];} $sep=', '; } Link to comment https://forums.phpfreaks.com/topic/49598-solved-how-to-have-two-arrays-in-a-foreach/#findComment-243262 Share on other sites More sharing options...
JSHINER Posted May 2, 2007 Author Share Posted May 2, 2007 Ok that works great - EXCEPT: If there is only ONE, it displays ", Text" Link to comment https://forums.phpfreaks.com/topic/49598-solved-how-to-have-two-arrays-in-a-foreach/#findComment-243264 Share on other sites More sharing options...
Agum Posted May 2, 2007 Share Posted May 2, 2007 Oops, enclose the line $sep = ', '; within the previous if block. if($y == $z['Short']) { echo $sep,$z['Long']; $sep=', ';} that should fix it. Link to comment https://forums.phpfreaks.com/topic/49598-solved-how-to-have-two-arrays-in-a-foreach/#findComment-243268 Share on other sites More sharing options...
JSHINER Posted May 2, 2007 Author Share Posted May 2, 2007 Perfect! Thank you! Link to comment https://forums.phpfreaks.com/topic/49598-solved-how-to-have-two-arrays-in-a-foreach/#findComment-243271 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.