Jump to content

is this foreach thingy possible?


real_illusions

Recommended Posts

To have 2 foreach statements combined into 1?Likeforeach ($arrayone[0] as $onekey => $onevalue) && ($arraytwo[0] as $twokey => $twovalue) {// magic goes here}As that gives me an error:Parse error: syntax error, unexpected T_BOOLEAN_AND

Link to comment
Share on other sites

though i am not sure the purpose of it ...foreach is not a logical structure where you can use any logical operators (&&). its basicaly a loop which will iterate through your array. i think so it is giving you a parsing error.

Link to comment
Share on other sites

What's wrong with having two nested foreach loops? Is it that you're trying to access the same indexes at once?If so, consider using a for loop instead. Something like:

for($i1=0, $i2=0;  ($array1available = $i1 < count($array1)) || ($array2available = $i2 < count($array2)); $i1++, $i2++) {	if ($array1available && $array2available) {		//Whatever's common for according array members		//Access such members with either $i1 or $i2	}else {		if ($array1available) {			//Whatever's common for extra array1 members			//Access such members with $i1		}else {			//Whatever's common for extra array2 members			//Access such members with $i2		}	}}

Link to comment
Share on other sites

Good catch. Perhaps an initialization is in order:

for($i1=0, $i2=0, $array1available = true, $array2available = true;  ($array1available = $i1 < count($array1)) || ($array2available = $i2 < count($array2)); $i1++, $i2++) {	if ($array1available && $array2available) {		//Whatever's common for according array members		//Access such members with either $i1 or $i2	}else {		if ($array1available) {			//Whatever's common for extra array1 members			//Access such members with $i1		}else {			//Whatever's common for extra array2 members			//Access such members with $i2		}	}}

Link to comment
Share on other sites

I don't think that kind of for loop will work. At least not like you're intending. Now the if will always be true if the first expression evaluates to true..........wait.........looking at that again, you're testing an assignment in the for loop's condition. Wouldn't that produce an infinite loop? Isn't the return value of an assignment always true?

Link to comment
Share on other sites

Wouldn't that produce an infinite loop? Isn't the return value of an assignment always true?
No. It's whatever value was assigned, so if the value is false, the whole check itself would evaluate to false.
Link to comment
Share on other sites

No. It's whatever value was assigned, so if the value is false, the whole check itself would evaluate to false.
Oh. Ok, I stand corrected. :)But still. PHP stops checking conditionals in an || operation if the first one is true. So if $array1Available is true, $array2Available will never be reassigned, meaning it will remain whatever value it is initialized to. So if it's initialized to true, and the first conditional sets $array1Available to true, even if the second conditional would set $array2Available to false, it will remain true and the if statement will evaluate to true even though it shouldn't.
Link to comment
Share on other sites

OK, this time I actually tested it, and you're right... the second condition is sort of making a problem. The only solution is to explicitly reevaluate the second condition every time, like:

for ($i1 = 0, $i2 = 0, $array1l = count($array1), $array2l = count($array2); ($array1available = $i1 < $array1l) || ($i2 < $array2l); $i1++, $i2++) {	if ($array1available && $i2 < $array2l) {		//Whatever's common for according array members		//Access such members with either $i1 or $i2		echo $array1[$i1], $array2[$i2];	}else {		if ($array1available) {			//Whatever's common for extra array1 members			//Access such members with $i1			echo $array1[$i1];		}else {			//Whatever's common for extra array2 members			//Access such members with $i2			echo $array2[$i2];		}	}}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...