us66mo Posted yesterday at 04:20 PM Share Posted yesterday 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 yesterday at 04:21 PM by us66mo Quote Link to comment Share on other sites More sharing options...
requinix Posted yesterday at 04:24 PM Share Posted yesterday 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 Share on other sites More sharing options...
us66mo Posted yesterday at 04:41 PM Author Share Posted yesterday 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 Share on other sites More sharing options...
mac_gyver Posted yesterday at 04:42 PM Share Posted yesterday 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 Share on other sites More sharing options...
Solution gizmola Posted 20 hours ago Solution Share Posted 20 hours ago So you want something like this: foreach ($tokenArr as $val) { //... current code // bottom of foreach loop if ($errMess !== '') { break; } } 1 Quote Link to comment Share on other sites More sharing options...
us66mo Posted 13 hours ago Author Share Posted 13 hours ago 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 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.