我得到“未定义偏移量”错误,从索引20开始,如下所示:
<b>Notice</b>: Undefined offset: 20 in <b>/var/www/sso/html/wp-content/plugins/auto-login/papex-auto-login.php</b> on line <b>214</b><br />
<br />
<b>Notice</b>: Undefined offset: 21 in <b>/var/www/sso/html/wp-content/plugins/auto-login/papex-auto-login.php</b> on line <b>214</b><br />
<br />
<b>Notice</b>: Undefined offset: 22 in <b>/var/www/sso/html/wp-content/plugins/auto-login/papex-auto-login.php</b> on line <b>214</b><br />
<br />我的数组$match (以下)有20个索引。我的SQL问题的输出是正确的-我已多次检查它。对于print_r,foreach循环的输出是echo $value->meta_key。
while循环似乎贯穿整个$match数组,但不会终止。这就是为什么我认为它开始产生“未定义的”偏移,从20开始。
我做错了什么;如果是正确的话,为什么代码没有退出while循环?
// Get user id
$db_user_id = $wpdb->get_row("SELECT ID FROM $table_users WHERE user_email = '$user_email'");
// Get user result
$results = $wpdb->get_results("SELECT * FROM $table_usermeta WHERE user_id = '$db_user_id->ID'");
$match = array(
"billing_country",
"billing_first_name",
"billing_last_name",
"billing_company",
"billing_address_1",
"billing_address_2",
"billing_city",
"billing_state",
"billing_postcode",
"billing_email",
"billing_phone",
"shipping_country",
"shipping_first_name",
"shipping_last_name",
"shipping_company",
"shipping_address_1",
"shipping_address_2",
"shipping_city",
"shipping_state",
"shipping_postcode"
);
foreach($results as $value)
{
$check = TRUE;
$i = 0;
while($check == TRUE)
{
if($match[$i] == $value->meta_key)
{
echo $i . ' ';
echo ' inne ';
$check = FALSE;
break;
}
$i++;
}
}发布于 2015-05-12 11:26:27
您应该检查值$match[$i]是否存在。很明显,错误消息的出现是因为它有时不会。
所以,你可以这样做:
if(isset($match[$i]) && $match[$i] == $value->meta_key) {
...
}或者,您可以用以下内容替换foreach循环中的完整部分:
for($i=0; $i<count($match); $i++) {
if($match[$i] == $value->meta_key) {
...
break;
}
}这样你就不会越界了。
您所做的错误主要是,您离开while循环的条件只有在匹配时才捕获,而不是在到达数组的末尾时捕获(您从未对此进行过测试)。
发布于 2015-05-12 11:28:33
显然,$value->meta_key并不等于任何$match项,因此if不会对循环进行break,$i的增量超过$match长度。
发布于 2015-05-12 12:00:49
只需在while循环中添加一个条件即可。
while($check == TRUE && $i< count($match))
{
if($match[$i] == $value->meta_key)
{
echo $i . ' ';
echo ' inne ';
$check = FALSE;
break;
}
$i++;
}https://stackoverflow.com/questions/30189330
复制相似问题