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? Quote 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) Quote 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? Quote 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=', '; Quote 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," Quote 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']) : ()); } } ?> Quote 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. Quote 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=', '; } Quote 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" Quote 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. Quote 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! Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.