while 循环,未定义的偏移量
While-loop, undefined offset
我收到 "Undefined offset" 错误,从索引 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 开始产生 "Undefined" 偏移量。
我做错了什么;为什么 - 如果它是正确的 - 代码没有退出 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++;
}
}
您应该检查值 $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 循环的条件只有在你有匹配时才会被捕获,而不是在到达数组末尾时(你从未测试过)。
显然 $value->meta_key
不等于任何 $match
项,因此 if
不 break
循环并且 $i
递增超过 $match
长度。
只需在 while 循环中添加一个条件
while($check == TRUE && $i< count($match))
{
if($match[$i] == $value->meta_key)
{
echo $i . ' ';
echo ' inne ';
$check = FALSE;
break;
}
$i++;
}
让它变得简单:
// This line only for test
$results = array((object) array('meta_key'=>'shipping_state'), (object) array('meta_key'=>'bla-bla'));
foreach($results as $value)
{
if (false !== ($i = array_search($value->meta_key, $match))) {
echo $i . ' ';
echo ' inne ';
}
}
我收到 "Undefined offset" 错误,从索引 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 开始产生 "Undefined" 偏移量。
我做错了什么;为什么 - 如果它是正确的 - 代码没有退出 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++;
}
}
您应该检查值 $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 循环的条件只有在你有匹配时才会被捕获,而不是在到达数组末尾时(你从未测试过)。
显然 $value->meta_key
不等于任何 $match
项,因此 if
不 break
循环并且 $i
递增超过 $match
长度。
只需在 while 循环中添加一个条件
while($check == TRUE && $i< count($match))
{
if($match[$i] == $value->meta_key)
{
echo $i . ' ';
echo ' inne ';
$check = FALSE;
break;
}
$i++;
}
让它变得简单:
// This line only for test
$results = array((object) array('meta_key'=>'shipping_state'), (object) array('meta_key'=>'bla-bla'));
foreach($results as $value)
{
if (false !== ($i = array_search($value->meta_key, $match))) {
echo $i . ' ';
echo ' inne ';
}
}