简体   繁体   中英

How to solve a PHP Fatal error: Uncaught Error: Call to undefined function each()?

Since moving to PHP 8 the code below gave me the error 'PHP Fatal error: Uncaught Error: Call to undefined function each()...'

public function Seek($ArrangementID)
{
    $this->populate();
    $bFound= false;
    reset($this->Arrangementen);
    while (!$bFound && (list($i, $oArrangement) = each ($this->Arrangementen)))
    {
        $bFound= ($oArrangement->ArrangementID== $ArrangementID);
        if ($bFound)
        $this->Current_Elm= $i;
    };
    $this->EOF= !$bFound;
    $this->setFieldValues();
    return($bFound);
}

Following some other posts with solutions to this error I changed a part of the while-list-each to: foreach ($this->Arrangementen as $i => $oArrangement)

public function Seek($ArrangementID)
{
    $this->populate();
    $bFound= false;
    reset($this->Arrangementen);
    foreach ($this->Arrangementen as $i => $oArrangement)
    {
        $bFound= ($oArrangement->ArrangementID== $ArrangementID);
        if ($bFound)
        $this->Current_Elm= $i;
    };
    $this->EOF= !$bFound;
    $this->setFieldValues();
    return($bFound);
}

However, how to incorporate the first part of the while function: "?$bFound &&" into the loop?

while (!$bFound && (list($i, $oArrangement) = each ($this->Arrangementen)))

Any ideas?

You could break out of the loop when you find your element.

public function Seek($ArrangementID)
{
    $this->populate();
    $bFound= false;
    reset($this->Arrangementen);
    foreach ($this->Arrangementen as $i => $oArrangement)
    {
        $bFound= ($oArrangement->ArrangementID== $ArrangementID);
        if ($bFound){
            $this->Current_Elm= $i;
            break; // Leave the loop!
        }
    };
    $this->EOF= !$bFound;
    $this->setFieldValues();
    return($bFound);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM