etrader Posted September 2, 2011 Share Posted September 2, 2011 I have two arrays as $array1=array("word1","word2","word3"); $array2=array("term1","term2","term3"); How can I have a foreach loop to echo "word1-term1", "word2-term2", .... I mean having echo "$array1[0]-$array2[0]"; but in a foreach loop Quote Link to comment https://forums.phpfreaks.com/topic/246249-how-to-load-two-arrays-in-a-foreach-loop/ Share on other sites More sharing options...
etrader Posted September 2, 2011 Author Share Posted September 2, 2011 I am thinking of running foreach for the first array; then bringing the value of second array by $array2. But I hope to find a more standard method for this. Quote Link to comment https://forums.phpfreaks.com/topic/246249-how-to-load-two-arrays-in-a-foreach-loop/#findComment-1264631 Share on other sites More sharing options...
voip03 Posted September 2, 2011 Share Posted September 2, 2011 you use array_combine http://php.net/manual/en/function.array-combine.php Quote Link to comment https://forums.phpfreaks.com/topic/246249-how-to-load-two-arrays-in-a-foreach-loop/#findComment-1264640 Share on other sites More sharing options...
etrader Posted September 2, 2011 Author Share Posted September 2, 2011 subtle trick! THANKS Quote Link to comment https://forums.phpfreaks.com/topic/246249-how-to-load-two-arrays-in-a-foreach-loop/#findComment-1264643 Share on other sites More sharing options...
etrader Posted September 2, 2011 Author Share Posted September 2, 2011 But array-combine creates an array by using one array for keys and another for its values. Not both values are inside the foreach loop to be used for echo or other processing. Quote Link to comment https://forums.phpfreaks.com/topic/246249-how-to-load-two-arrays-in-a-foreach-loop/#findComment-1264644 Share on other sites More sharing options...
skwap Posted September 2, 2011 Share Posted September 2, 2011 Try this way.. <?php $array1=array("word1","word2","word3"); $array2=array("term1","term2","term3"); for($i=0; $i<count($array1); $i++) { echo ''.$array1[$i].' - '.$array2[$i].'<br/>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/246249-how-to-load-two-arrays-in-a-foreach-loop/#findComment-1264656 Share on other sites More sharing options...
etrader Posted September 2, 2011 Author Share Posted September 2, 2011 Thanks, it works perfectly Quote Link to comment https://forums.phpfreaks.com/topic/246249-how-to-load-two-arrays-in-a-foreach-loop/#findComment-1264673 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.