2

I get "Undefined offset" errors, starting from index 20 as shown in the 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. The output from my SQL question is correct – I have checked it multiple times. With print_r, the output from the foreach loop is echo $value->meta_key.

It seems that the while loop goes through the whole $match array, but won't terminate. Thats why I think it starts producing the "Undefined" offsets, starting from 20.

What am I doing wrong; how come – if its right – that the code doesn't exit the while loop?

// 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++;   
    }
}

4 Answers 4

2

You should check if the value $match[$i] exists. Clearly your error messages occur because it sometimes doesn't.

So, you could either do:

if(isset($match[$i]) && $match[$i] == $value->meta_key) {
    ...
}

Or you could replace the complete part inside your foreach loop with this:

for($i=0; $i<count($match); $i++) {
    if($match[$i] == $value->meta_key) {
        ...
        break;
    }
}

This way you make sure you never get out of bounds.

What you did wrong was mostly that your condition for leaving the while loop only ever caught when you had a match, but not when the end of the array was reached (you never tested for that).

Sign up to request clarification or add additional context in comments.

Comments

0

Apparently $value->meta_key doesn't equal any of the $match items, so the if doesn't break the loop and $i gets incremented past the $match length.

Comments

0

Just add one condition in your while loop

while($check == TRUE && $i< count($match))
{
    if($match[$i] == $value->meta_key)
    {
        echo $i . ' ';
        echo ' inne ';
        $check = FALSE;
        break;
    }   

    $i++;   
}

Comments

0

Make it easy:

// 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 ';
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.