us66mo Posted Friday at 04:20 PM Share Posted Friday at 04:20 PM (edited) I am updating my website to PHP 8.2 and I am replacing, among other things, all each() functions. Most are quite easy to replace, but this code has me stumped. How do I replace each() here with a foreach() function. I tried double nesting 2 foreach() functions, but it didn't work. while (($errMess == "") && (list(,$val) = each($tokenArr))) { ... execution commands ... Any help would be greatly appreciated! Thanks! Edited Friday at 04:21 PM by us66mo Quote Link to comment https://forums.phpfreaks.com/topic/325495-foreach-function-to-replace-each-function-in-while-loop/ Share on other sites More sharing options...
requinix Posted Friday at 04:24 PM Share Posted Friday at 04:24 PM (foreach isn't a function) It'd be easier to show if you posted the rest of the code instead of just a couple lines, but basically, replace the while loop with a foreach loop, and then add into it a little logic for $errMess. If that still doesn't make sense, post what you came up with (as well as the original code) so we can see what happened and not have to guess about it. 1 Quote Link to comment https://forums.phpfreaks.com/topic/325495-foreach-function-to-replace-each-function-in-while-loop/#findComment-1640150 Share on other sites More sharing options...
us66mo Posted Friday at 04:41 PM Author Share Posted Friday at 04:41 PM Thanks for the response! Here is the code for the while loop. "each" is combined with $errMess==" " : while (($errMess == "") && (list(,$val) = each($tokenArr))) { $this->log->notice(sprintf($SearchEngineConst['TokenInSequenceMsg'],$val["item"])); if ($val["type"] == "operand") { $this->log->notice(sprintf($SearchEngineConst['OperandPushedToArrayMsg'],$val["item"])); $tableName = getOrigTmpName(); if ($val["UnaryOp"] == "") { if (preg_match("/^\{(.*)\}$/",$val["item"],$arr)) { $val["item"] = $arr[1]; $this->getSelection($val["item"],$path,"folder",$tableName,$errMess); } else { $this->getSelection($val["item"],$path,"word",$tableName,$errMess); } } else { if (preg_match("/^\{(.*)\}$/",$val["item"],$arr)) { $val["item"] = $arr[1]; $this->getNotSelection($val["item"],$path,"folder",$tableName,$errMess); } else { $this->getNotSelection($val["item"],$path,"word",$tableName,$errMess); } } if ($errMess == "") { $val["item"] = $tableName; array_push($operandStack,$val); } } else if ($val["type"] == "operator") { $op1 = array_pop($operandStack); $op2 = array_pop($operandStack); if (($op1 == NULL) || ($op2 == NULL)) { $errMess = $errMessPrefix.$SearchEngineConst['EmptyOperandStackErrMsg']; $this->log->error($errMess); } if ($errMess == "") { $this->log->notice(sprintf($SearchEngineConst['ExecuteOperationMsg'],$val[item])); $this->log->notice(sprintf($SearchEngineConst['OperationInfoMsg'],$op1,$val[item],$op2)); $this->executeOperation($op1,$op2,$val["item"],$resOperand,$errMess); // old: $this->executeOperation($op1,$op2,$val["item"],&$resOperand,$errMess); } if ($errMess == "") { $this->log->notice(sprintf($SearchEngineConst['OperationResultMsg'],$resOperand[item])); array_push($operandStack,$resOperand); } } else { $errMess = $errMessPrefix.sprintf($SearchEngineConst['TokenTypeUnknownErrMsg'],$val[type]); $this->log->error($errMess); } } //END WHILE LOOP Quote Link to comment https://forums.phpfreaks.com/topic/325495-foreach-function-to-replace-each-function-in-while-loop/#findComment-1640151 Share on other sites More sharing options...
mac_gyver Posted Friday at 04:42 PM Share Posted Friday at 04:42 PM the code's looping until there's an error (while no error). just use a foreach loop, with a break when the error occurs OR perhaps loop over all the data, producing an array of errors, then use the array of errors after the end of the loop? what does a sample of the data look like, what exactly are you doing with it, what do you want to do upon the first error or do you want to check every entry for errors? Quote Link to comment https://forums.phpfreaks.com/topic/325495-foreach-function-to-replace-each-function-in-while-loop/#findComment-1640152 Share on other sites More sharing options...
Solution gizmola Posted Friday at 08:40 PM Solution Share Posted Friday at 08:40 PM So you want something like this: foreach ($tokenArr as $val) { //... current code // bottom of foreach loop if ($errMess !== '') { break; } } 1 Quote Link to comment https://forums.phpfreaks.com/topic/325495-foreach-function-to-replace-each-function-in-while-loop/#findComment-1640168 Share on other sites More sharing options...
us66mo Posted Saturday at 03:15 AM Author Share Posted Saturday at 03:15 AM 6 hours ago, gizmola said: So you want something like this: foreach ($tokenArr as $val) { //... current code // bottom of foreach loop if ($errMess !== '') { break; } } Excellent, thank you! Interesting that foreach (array_keys($tokenArr) as $val) I used elsewhere for similar code to be replaced did not work, but your code did. Quote Link to comment https://forums.phpfreaks.com/topic/325495-foreach-function-to-replace-each-function-in-while-loop/#findComment-1640188 Share on other sites More sharing options...
gizmola Posted Saturday at 09:35 PM Share Posted Saturday at 09:35 PM 19 hours ago, us66mo said: Excellent, thank you! Interesting that foreach (array_keys($tokenArr) as $val) I used elsewhere for similar code to be replaced did not work, but your code did. Well, yes, because array_keys will provide you all the keys in the array at the first dimension, which is probably not what you wanted. If you want access to the key in the array you can use: foreach($tokenArr as $key => $val) { } Example: $fruits = array('a' => 'apple', 'b' => 'banana', 'o' => 'orange'); foreach ($fruits as $key => $val) { echo "The $key is one $val \n"; } Output is: The a is one apple The b is one banana The o is one orange Quote Link to comment https://forums.phpfreaks.com/topic/325495-foreach-function-to-replace-each-function-in-while-loop/#findComment-1640214 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.