英文:
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( '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;
}
}
}
}
I believe that it should be changed for 'foreach' but I don't know how to do it correctly.
Pls help me
答案1
得分: 1
each()
函数 已于 PHP 7.2.0 版本开始弃用,并在 PHP 8.0.0 版本中移除。强烈不建议依赖此函数。
还请注意,each()
函数通常与 list()
一起使用以遍历数组。
因此,替代方法是对数组的 键 / 值 对使用 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;
}
}
}
}
现在应该可以工作了。
英文:
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->element_activities[ $tag ]
array.
So the correct revisited working code should be:
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;
}
}
}
}
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( '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;
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论