我继承的这个站点网站正在转移到PHP 7.0环境中,并且在运行兼容性检查器(作为WP)之后,我发现了几个错误,其中使用.each()代替foreach ()循环。但是,我从未使用过.each()循环,并且在查看了一些文档和stackoverflows之后,我只是没有将头转向转换。我想知道是否有人可以帮助我解决这个问题。通过查看代码,我真正看到的唯一一件事是$k
是数组位置的指示吗?所以,我想也许是如果我尝试将其设置为数字并让其递增?
public function _sanitize_data_array($submited_data)
{
$in = array(&$submited_data);
while ( list($k,$v) = each($in) ) {
foreach ( $in as $key => $val ) {
if ( !is_array($val) ) {
$in[$k][$key] = trim(stripslashes($val));
$in[$k][$key] = wp_kses($in[$k][$key], $allowed_html=array());
$in[$k][$key] = esc_attr($in[$k][$key]);
$in[$k][$key] = trim($in[$k][$key]);
continue;
};
if ( is_array($val) ) {
$in[$k][$key] = array_filter($val);
};
$in[] =& $in[$k][$key];
};
};
unset($in);
return $submited_data;
}
public function scroll()
{
$raw_data = $_POST;
$data = $this->_sanitize_data_array($raw_data);
$properties = $this->_get_properties($data);
$prop_str = implode('', $properties);
$response_code = ( count( $properties ) > 0 ) ? '1' : '-1';
$response['code'] = $response_code;
$response['properties'] = $prop_str ;
$response['next_page'] = $this->next_page;
die(json_encode($response));
}
public function _sanitize_data_array($submited_data)
{
$in = array(&$submited_data);
$k = 1;
foreach ( $in as $key => $val ) {
if ( !is_array($val) ) {
$in[$k][$key] = trim(stripslashes($val));
$in[$k][$key] = wp_kses($in[$k][$key], $allowed_html=array());
$in[$k][$key] = esc_attr($in[$k][$key]);
$in[$k][$key] = trim($in[$k][$key]);
continue;
};
if ( is_array($val) ) {
$in[$k][$key] = array_filter($val);
};
$in[] =& $in[$k][$key];
$k++;
};
unset($in);
return $submited_data;
}
答案 0 :(得分:2)
试图更改此代码的真正问题不是来自 \fmtnum
,而是来自行
$k
$in[] =& $in[$k][$key];.
循环将数组while
用作队列。这是不使用递归函数的递归的常见实现。
仅将$in
组合到each
循环中才能允许此操作而不是foreach。
我唯一的建议是忽略警告或使用递归函数。如果您担心有一天将删除每个函数,不必担心每个函数的构建都太简单了:
while/for/do while
答案 1 :(得分:0)
忘记了循环中的内容,您必须替换 while 循环,该循环使用每个循环将$in
数组的每个元素的 key 分配给{ {1}}并具有等效的 foreach 循环。
$k
它应该像这样:
while ( list($k,$v) = each($in) ) {
foreach ( $in as $key => $val ) {
// Some code here
};
};
在原始代码中, while 循环采用数组第一个元素的键和值,并将其分配给变量foreach ( $in as $k => $v ) {
foreach ( $in as $key => $val ) {
// Some code here
};
};
和$k
,将数组的光标移动到下一个元素,并评估块内的代码。重复直到没有剩余元素。您可以像之前显示的那样使用 foreach 进行相同的操作。