2

I am learning arrays and put this together...it works meaning I get the results I want but I am getting undefined offset errors.

$err_array = array();
$err_array[0] = array('Step 1 and 2 are empty.','chu'); 
$err_array[1] = array('Step 1 is empty (but optional) a','Step 1 is empty (but optional)'); 
$err_array[2] = array(' Step 2 is empty (and required)','Step 2 is empty (and required)'); 
$err_array[3] = array(' Step 3 is empty (and required)','Step 3 is empty (and required'); 
$err_array[4] = array(' Step 4 is empty (and optional)','Step 4 is empty (and optional)'); 
        
    # Set text color of error msg
    $counter = 0;   
        
# line 16   while (is_array($err_array[$counter]) ) {   
                $err_[$counter]     = ''.$err_array[$counter][0].''; 
# line 18       $err__[$counter]    = ''.$err_array[$counter][1].'';
            $counter++; 
            }

Here is the erors:

Notice: Undefined offset: 5 in /nfs/c08/h04/mnt/124078/domains/yourinternetfootprint.com/html/wp-content/plugins/wordpress_meta_box_sample_files/includes/template_yif_ealfm_get_rss_feed_parameters.php on line 16

Notice: Undefined offset: 1 in /nfs/c08/h04/mnt/124078/domains/yourinternetfootprint.com/html/wp-content/plugins/wordpress_meta_box_sample_files/includes/template_yif_ealfm_get_rss_feed_parameters.php on line 18

I know there is a cleaner way to approach taking the errors messages and assigning them to varables and wrapping some css around them...but as I said I am learning.

1
  • Check if the offset you're trying to read actually exists. while (isset($err_array[$counter]) && is_array($err_array[$counter]) ). Commented Jun 2, 2012 at 4:28

1 Answer 1

1

To properly iterate over $err_array you should use foreach:

foreach ($err_array as $counter => $errors) {
    if (isset($errors[0])) { // make sure $errors[0] exists
        $err_[$counter] = $errors[0];
    }
    if (isset($errors[1])) { // make sure $errors[1] exists
        $err__[$counter] = $errors[1];
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Jack, that makes so much more since...thanks. It worked and now the error is gone.
@jason that's good to know :) please accept the answer when you're happy with it.

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.