Brandon_R Posted September 29, 2009 Share Posted September 29, 2009 Hello guys i am having a bit of trouble looping through arrays. Notice i said arrays I was just wondering if i can do this $array1 = explode($_POST['array1']); $array2 = explode($_POST['array2']); foreach ($array1 AS $array1exploded AND $array2 AS $array2exploded) { code here to play with the arrays } If not could you guys guide me on how to do it. Quote Link to comment https://forums.phpfreaks.com/topic/175993-can-i-do-this-if-not-how/ Share on other sites More sharing options...
Alex Posted September 29, 2009 Share Posted September 29, 2009 Nested foreach loops: foreach($arr1 as $part1) { foreach($arr2 as $part2) { //Stuff } } Quote Link to comment https://forums.phpfreaks.com/topic/175993-can-i-do-this-if-not-how/#findComment-927352 Share on other sites More sharing options...
.josh Posted September 30, 2009 Share Posted September 30, 2009 hmm not sure if OP wants nested, but rather same level looping. If the array keys are the same, you can do this: foreach($array1 as $key => $val) { echo $val; // echo current $array1 value echo $array2[$key]; // echo current $array2 value } if one is a numeric array ($array1) and one is an associative array ($array2), you can do this: $key = 0; foreach ($array2 as $val) { echo $val; // echo current $array2 (associative) value echo $array1[$key]; // echo current $array1 (numeric) value $key++; } If they are both associative but have different keys, you can do this: foreach ($array1 as $val) { echo $val; // echo current $array1 value echo current($array2); // echo current $array2 value next($array2); } of course, all of these examples assume that both of the arrays have the same number of elements... Quote Link to comment https://forums.phpfreaks.com/topic/175993-can-i-do-this-if-not-how/#findComment-927386 Share on other sites More sharing options...
Brandon_R Posted September 30, 2009 Author Share Posted September 30, 2009 @Alex Seems like im going to try your method. I am going to do some error checking for each foreach. I would like to know which number the foreach loop is on so i can output Input for example Number 7 was incorrectly formatted. Is there any native way to do that or do i have to add counter++ in the foreach to determine the number it is at. Quote Link to comment https://forums.phpfreaks.com/topic/175993-can-i-do-this-if-not-how/#findComment-927410 Share on other sites More sharing options...
Alex Posted September 30, 2009 Share Posted September 30, 2009 Are you only checking one variable? Because that's what it sounds like from that description. Because you want to know the index of the element if you should use a for(): for($i = 0;$i < count($arr);$i++) { echo $arr[$i]; } Quote Link to comment https://forums.phpfreaks.com/topic/175993-can-i-do-this-if-not-how/#findComment-927411 Share on other sites More sharing options...
Brandon_R Posted September 30, 2009 Author Share Posted September 30, 2009 I am checking for all the variables to make sure they exist before saving. Quote Link to comment https://forums.phpfreaks.com/topic/175993-can-i-do-this-if-not-how/#findComment-927962 Share on other sites More sharing options...
Alex Posted September 30, 2009 Share Posted September 30, 2009 Do you mean you're checking all the elements of more than one array? Can you explain a bit better please. Quote Link to comment https://forums.phpfreaks.com/topic/175993-can-i-do-this-if-not-how/#findComment-927963 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.