php - While-loop, undefined offset -
i "undefined offset" errors, starting index 20 shown in following output:
<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 />
my array $match
- below - has 20 indexes. output sql question correct – have checked multiple times. print_r
, output foreach loop echo $value->meta_key
.
it seems while
loop goes through whole $match
array, won't terminate. thats why think starts producing "undefined" offsets, starting 20.
what doing wrong; how come – if right – code doesn't exit while
loop?
// user id $db_user_id = $wpdb->get_row("select id $table_users user_email = '$user_email'"); // user result $results = $wpdb->get_results("select * $table_usermeta 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 $value) { $check = true; $i = 0; while($check == true) { if($match[$i] == $value->meta_key) { echo $i . ' '; echo ' inne '; $check = false; break; } $i++; } }
you should check if $match[$i]
exists. error mesages occur because doesn't.
so, either do:
if(isset($match[$i] && $match[$i] == $value->meta_key) { ... }
or replace copmplete part inside foreach loop this:
for($i=0; $i<count($match); $i++) { if($match[$i] == $value->meta_key) { ... break; } }
this way make sure never out of bounds.
what did wrong condition leaving while loop ever caught when had match, not when end of array reached (you never tested that).
Comments
Post a Comment