如何在这种情况下正确重写未定义的函数 each()?

huangapple go评论67阅读模式
英文:

How to rewrite the undefined function each() correctly in this case?

问题

在旧的WordPress网站上更新PHP时出现了一个未捕获的错误:在class-vc-mapper.php的第186行中调用未定义的函数each()。

public function callElementActivities($tag) {
    do_action('vc_mapper_call_activities_before');
    if (isset($this->element_activities[$tag])) {
        foreach ($this->element_activities[$tag] as $activity) { // << 这一行
            list($method, $params) = $activity;
            switch ($method) {
                case 'drop_param':
                    WPBMap::dropParam($params['name'], $params['attribute_name']);
                    break;
                case 'add_param':
                    WPBMap::addParam($params['name'], $params['attribute']);
                    break;
                case 'mutate_param':
                    WPBMap::mutateParam($params['name'], $params['attribute']);
                    break;
                case 'drop_shortcode':
                    WPBMap::dropShortcode($params['name']);
                    break;
                case 'modify':
                    WPBMap::modify($params['name'], $params['setting_name'], $params['value']);
                    break;
            }
        }
    }
}

我相信应该将它更改为'foreach',但我不知道如何正确执行。请帮我:D

英文:

Updating PHP on old WordPress website got an Uncaught Error: Call to undefined function each() in 186 line of class-vc-mapper.php

public function callElementActivities( $tag ) {
		do_action( &#39;vc_mapper_call_activities_before&#39; );
		if ( isset( $this-&gt;element_activities[ $tag ] ) ) {
				while ( $activity = each( $this-&gt;element_activities[ $tag ] ) ) {  &lt;&lt; line
				list ( $method, $params ) = $activity[1];
				switch ( $method ) {
					case &#39;drop_param&#39;:
						WPBMap::dropParam( $params[&#39;name&#39;], $params[&#39;attribute_name&#39;] );
						break;
					case &#39;add_param&#39;:
						WPBMap::addParam( $params[&#39;name&#39;], $params[&#39;attribute&#39;] );
						break;
					case &#39;mutate_param&#39;:
						WPBMap::mutateParam( $params[&#39;name&#39;], $params[&#39;attribute&#39;] );
						break;
					case &#39;drop_shortcode&#39;:
						WPBMap::dropShortcode( $params[&#39;name&#39;] );
						break;
					case &#39;modify&#39;:
						WPBMap::modify( $params[&#39;name&#39;], $params[&#39;setting_name&#39;], $params[&#39;value&#39;] );
						break;
				}
			}
		}

	}

I believe that it should be changed for 'foreach' but I don't know how to do it correctly.
Pls help me 如何在这种情况下正确重写未定义的函数 each()?

答案1

得分: 1

each() 函数 已于 PHP 7.2.0 版本开始弃用,并在 PHP 8.0.0 版本中移除。强烈不建议依赖此函数。

还请注意,each() 函数通常与 list() 一起使用以遍历数组。

因此,替代方法是对数组的 / 对使用 foreach() 函数(但不仅限于),以遍历 $this-&gt;element_activities[ $tag ] 数组。

因此,正确的修订后工作代码应为:

public function callElementActivities( $tag ) {
    do_action( &#39;vc_mapper_call_activities_before&#39; );

    if ( isset( $this-&gt;element_activities[ $tag ] ) ) {
        foreach ( $this-&gt;element_activities[ $tag ] as $method =&gt; $params ) {
            switch ( $method ) {
                case &#39;drop_param&#39;:
                    WPBMap::dropParam( $params[&#39;name&#39;], $params[&#39;attribute_name&#39;] );
                    break;
                case &#39;add_param&#39;:
                    WPBMap::addParam( $params[&#39;name&#39;], $params[&#39;attribute&#39;] );
                    break;
                case &#39;mutate_param&#39;:
                    WPBMap::mutateParam( $params[&#39;name&#39;], $params[&#39;attribute&#39;] );
                    break;
                case &#39;drop_shortcode&#39;:
                    WPBMap::dropShortcode( $params[&#39;name&#39;] );
                    break;
                case &#39;modify&#39;:
                    WPBMap::modify( $params[&#39;name&#39;], $params[&#39;setting_name&#39;], $params[&#39;value&#39;] );
                    break;
            }
        }
    }
}

现在应该可以工作了。

英文:

Note that each() function has been deprecated as of PHP 7.2.0, and REMOVED as of PHP 8.0.0. Relying on this function is highly discouraged.

Also note that each() function is typically used in conjunction with list() to traverse an array.

So the alternative is to use the foreach() function on the array key / value pairs (but not only the values), to traverse $this-&gt;element_activities[ $tag ] array.

So the correct revisited working code should be:

public function callElementActivities( $tag ) {
    do_action( &#39;vc_mapper_call_activities_before&#39; );

    if ( isset( $this-&gt;element_activities[ $tag ] ) ) {
        foreach ( $this-&gt;element_activities[ $tag ] as $method =&gt; $params ) {
            switch ( $method ) {
                case &#39;drop_param&#39;:
                    WPBMap::dropParam( $params[&#39;name&#39;], $params[&#39;attribute_name&#39;] );
                    break;
                case &#39;add_param&#39;:
                    WPBMap::addParam( $params[&#39;name&#39;], $params[&#39;attribute&#39;] );
                    break;
                case &#39;mutate_param&#39;:
                    WPBMap::mutateParam( $params[&#39;name&#39;], $params[&#39;attribute&#39;] );
                    break;
                case &#39;drop_shortcode&#39;:
                    WPBMap::dropShortcode( $params[&#39;name&#39;] );
                    break;
                case &#39;modify&#39;:
                    WPBMap::modify( $params[&#39;name&#39;], $params[&#39;setting_name&#39;], $params[&#39;value&#39;] );
                    break;
            }
        }
    }
}

IT should work now.

答案2

得分: 0

public function callElementActivities( $tag ) {
    do_action( 'vc_mapper_call_activities_before' );
    if ( isset( $this->element_activities[ $tag ] ) ) {
        foreach ( $this->element_activities[ $tag ] as $activity ) {
            list ( $method, $params ) = $activity;
            switch ( $method ) {
                case 'drop_param':
                    WPBMap::dropParam( $params['name'], $params['attribute_name'] );
                    break;
                case 'add_param':
                    WPBMap::addParam( $params['name'], $params['attribute'] );
                    break;
                case 'mutate_param':
                    WPBMap::mutateParam( $params['name'], $params['attribute'] );
                    break;
                case 'drop_shortcode':
                    WPBMap::dropShortcode( $params['name'] );
                    break;
                case 'modify':
                    WPBMap::modify( $params['name'], $params['setting_name'], $params['value'] );
                    break;
            }
        }
    }
}
英文:
public function callElementActivities( $tag ) {
    do_action( &#39;vc_mapper_call_activities_before&#39; );
    if ( isset( $this-&gt;element_activities[ $tag ] ) ) {
        foreach ( $this-&gt;element_activities[ $tag ] as $activity ) {
            list ( $method, $params ) = $activity;
            switch ( $method ) {
                case &#39;drop_param&#39;:
                    WPBMap::dropParam( $params[&#39;name&#39;], $params[&#39;attribute_name&#39;] );
                    break;
                case &#39;add_param&#39;:
                    WPBMap::addParam( $params[&#39;name&#39;], $params[&#39;attribute&#39;] );
                    break;
                case &#39;mutate_param&#39;:
                    WPBMap::mutateParam( $params[&#39;name&#39;], $params[&#39;attribute&#39;] );
                    break;
                case &#39;drop_shortcode&#39;:
                    WPBMap::dropShortcode( $params[&#39;name&#39;] );
                    break;
                case &#39;modify&#39;:
                    WPBMap::modify( $params[&#39;name&#39;], $params[&#39;setting_name&#39;], $params[&#39;value&#39;] );
                    break;
            }
        }
    }
}

huangapple
  • 本文由 发表于 2023年6月12日 21:44:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457289.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定