each() discontinued in PHP

Asked

Viewed 104 times

1

How can I solve the problem of each() obsolete (from php7.2) in the code below?

            foreach($inputarr as $arr) {
                $sql = ''; $i = 0;
                //Use each() instead of foreach to reduce memory usage -mikefedyk
                while(list(, $v) = each($arr)) {
                    $sql .= $sqlarr[$i];
                    // from Ron Baldwin <ron.baldwin#sourceprose.com>
                    // Only quote string types
                    $typ = gettype($v);
                    if ($typ == 'string') {
                        //New memory copy of input created here -mikefedyk
                        $sql .= $this->qstr($v);
                    } else if ($typ == 'double') {
                        $sql .= str_replace(',','.',$v); // locales fix so 1.1 does not get converted to 1,1
                    } else if ($typ == 'boolean') {
                        $sql .= $v ? $this->true : $this->false;
                    } else if ($typ == 'object') {
                        if (method_exists($v, '__toString')) {
                            $sql .= $this->qstr($v->__toString());
                        } else {
                            $sql .= $this->qstr((string) $v);
                        }
                    } else if ($v === null) {
                        $sql .= 'NULL';
                    } else {
                        $sql .= $v;
                    }
                    $i += 1;

                    if ($i == $nparams) {
                        break;
                    }
                } // while
  • use foreach, which need to use each

  • 1

    I removed my reply, since the @Sveen response is complete enough.

  • 1

    I edited the question, deprecated in Portuguese is different, the correct term is discontinued - read: https://answall.com/q/178138/3635#comment368172_178138

1 answer

3

Use foreach instead of while and each

foreach($arr as $v) {

Browser other questions tagged

You are not signed in. Login or sign up in order to post.