Brandon_R Posted October 1, 2009 Share Posted October 1, 2009 Hello guys can someone show me how to create a loop for this array echoing out the result. The array is $hello = array("hello", "guys", "hows", "it"); $goodbye = array("Its", "going", "find", "thanks"); $aiight = array("Good", "then", "thats", "well"); After the above array is run through the loop the result for each echo should be Hello Its Good Guys Going Then Hows find thats It thanks well. I tried nested foreach loops and the result was catastrophic. It was more like a factorial problem than matching the first element in the array with the first element in the other 2 arrays. Quote Link to comment https://forums.phpfreaks.com/topic/176213-how-do-i-create-a-loop-for-this/ Share on other sites More sharing options...
mikesta707 Posted October 1, 2009 Share Posted October 1, 2009 assuming the 3 arrays will always be the same length $hello = array("hello", "guys", "hows", "it"); $goodbye = array("Its", "going", "find", "thanks"); $aiight = array("Good", "then", "thats", "well"); for ($i = 0; $i < count($hello); $i++){ echo $hello[$i]." ".$goodbye[$i]." ".$aiight[$i]."<br />"; } if they aren't always the same size this won't work as expected Quote Link to comment https://forums.phpfreaks.com/topic/176213-how-do-i-create-a-loop-for-this/#findComment-928646 Share on other sites More sharing options...
cags Posted October 1, 2009 Share Posted October 1, 2009 <?php $count = count($hello); for($i = 0; $i < $count; $i++) { echo $hello[$i] . " " . $goodbye[$i] . " " . $aiight[$i] . '<br />'; } ?> EDIT: D'oh beaten to the punch, oh well, I think my $count method would be slightly more optimised. Quote Link to comment https://forums.phpfreaks.com/topic/176213-how-do-i-create-a-loop-for-this/#findComment-928648 Share on other sites More sharing options...
mikesta707 Posted October 1, 2009 Share Posted October 1, 2009 I don't see how it would be any more optimised Quote Link to comment https://forums.phpfreaks.com/topic/176213-how-do-i-create-a-loop-for-this/#findComment-928652 Share on other sites More sharing options...
Alex Posted October 1, 2009 Share Posted October 1, 2009 I don't see how it would be any more optimised I think he means because count($hello) won't have to be pre-preformed every loop. The time difference would be so minimal.. it really wouldn't matter. Personally I'd use Mikesta707's method only because there's no need for that additional variable / line when it's going to save 0.000001 second. The general fluctuations in execution time would be considerably larger than the time that might save. Quote Link to comment https://forums.phpfreaks.com/topic/176213-how-do-i-create-a-loop-for-this/#findComment-928655 Share on other sites More sharing options...
Brandon_R Posted October 2, 2009 Author Share Posted October 2, 2009 Is there any other way to do it? Quote Link to comment https://forums.phpfreaks.com/topic/176213-how-do-i-create-a-loop-for-this/#findComment-929337 Share on other sites More sharing options...
Alex Posted October 2, 2009 Share Posted October 2, 2009 Is there any other way to do it? What's wrong with those ways? Quote Link to comment https://forums.phpfreaks.com/topic/176213-how-do-i-create-a-loop-for-this/#findComment-929338 Share on other sites More sharing options...
Brandon_R Posted October 2, 2009 Author Share Posted October 2, 2009 Can't get it to work. Edit, i now see they have to contain the same amount of of values in the array so they can get matched. Ill see if i can write an error checking to stop if it doesn't match. Im new at PHP so ill need a little help. Quote Link to comment https://forums.phpfreaks.com/topic/176213-how-do-i-create-a-loop-for-this/#findComment-929340 Share on other sites More sharing options...
Philip Posted October 2, 2009 Share Posted October 2, 2009 Given they are the same lengths, with the same keys (in this case numerical).. no need for the count($array) in a for statement... thats why they made foreach $hello = array("hello", "guys", "hows", "it"); $goodbye = array("Its", "going", "find", "thanks"); $aiight = array("Good", "then", "thats", "well"); foreach($hello as $key => $value) { echo $value.' '.$goodbye[$key].' '.$aiight[$key].'<br>'; } Well - if they aren't the same length what do you want the script to do? Provide a blank space, or re-use another value somewhere? Quote Link to comment https://forums.phpfreaks.com/topic/176213-how-do-i-create-a-loop-for-this/#findComment-929343 Share on other sites More sharing options...
Brandon_R Posted October 2, 2009 Author Share Posted October 2, 2009 How about simply halt execution. Ill add my error message later on when im finalizing the script. Quote Link to comment https://forums.phpfreaks.com/topic/176213-how-do-i-create-a-loop-for-this/#findComment-929358 Share on other sites More sharing options...
Brandon_R Posted October 3, 2009 Author Share Posted October 3, 2009 Actually king phillip can you show me how to check each array to make sure they contain the same number of values and if they don't echo out an error message such as this is not an array. Quote Link to comment https://forums.phpfreaks.com/topic/176213-how-do-i-create-a-loop-for-this/#findComment-929621 Share on other sites More sharing options...
cags Posted October 3, 2009 Share Posted October 3, 2009 Not sure if it's the best way, but... <?php $length = count($array1); if(count($array2) != $length || count($array3) != $length) { echo 'Lengths do not match'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/176213-how-do-i-create-a-loop-for-this/#findComment-929631 Share on other sites More sharing options...
Philip Posted October 3, 2009 Share Posted October 3, 2009 Not sure if it's the best way, but... <?php $length = count($array1); if(count($array2) != $length || count($array3) != $length) { echo 'Lengths do not match'; } ?> Is just fine, just change the array variables to match your script. Quote Link to comment https://forums.phpfreaks.com/topic/176213-how-do-i-create-a-loop-for-this/#findComment-929840 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.