real_illusions Posted March 1, 2011 Share Posted March 1, 2011 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 More sharing options...
birbal Posted March 1, 2011 Share Posted March 1, 2011 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 More sharing options...
jeffman Posted March 1, 2011 Share Posted March 1, 2011 You can get what you need using each inside the loop. Notice how it auto-increments. Link to comment Share on other sites More sharing options...
boen_robot Posted March 2, 2011 Share Posted March 2, 2011 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 More sharing options...
ShadowMage Posted March 2, 2011 Share Posted March 2, 2011 Won't PHP skip the second expression if the first evaluates to true?If that is the case, then the if ($array1available && $array2available) will always be false. Link to comment Share on other sites More sharing options...
boen_robot Posted March 2, 2011 Share Posted March 2, 2011 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 More sharing options...
ShadowMage Posted March 2, 2011 Share Posted March 2, 2011 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 More sharing options...
boen_robot Posted March 2, 2011 Share Posted March 2, 2011 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 More sharing options...
ShadowMage Posted March 3, 2011 Share Posted March 3, 2011 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 More sharing options...
boen_robot Posted March 3, 2011 Share Posted March 3, 2011 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.