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

gcuhipw9  于 2023-06-21  发布在  WordPress
关注(0)|答案(2)|浏览(114)

在旧的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 ] ) ) {
                while ( $activity = each( $this->element_activities[ $tag ] ) ) {  << line
                list ( $method, $params ) = $activity[1];
                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

clj7thdc

clj7thdc1#

请注意,each() function在PHP 7.2.0中已弃用,在PHP 8.0.0中已删除。非常不鼓励依赖此功能。
还要注意,each()函数通常与list()结合使用,以遍历数组。
因此,另一种方法是在数组key/valuepairs (但不仅仅是values 上使用foreach()函数,以遍历$this->element_activities[ $tag ]数组。
因此,正确的重新访问的工作代码应该是:

public function callElementActivities( $tag ) {
    do_action( 'vc_mapper_call_activities_before' );

    if ( isset( $this->element_activities[ $tag ] ) ) {
        foreach ( $this->element_activities[ $tag ] as $method => $params ) {
            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;
            }
        }
    }
}

它现在应该工作。

lyfkaqu1

lyfkaqu12#

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;
            }
        }
    }
}

相关问题