Jump to content

foreach() function to replace each() function in while loop


us66mo
Go to solution Solved by gizmola,

Recommended Posts

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 by us66mo
Link to comment
Share on other sites

(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.

  • Like 1
Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.