Jason
Jason

Reputation: 462

Undefined offset error, new with building arrays

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.

Upvotes: 2

Views: 531

Answers (1)

Ja͢ck
Ja͢ck

Reputation: 173652

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];
    }
}

Upvotes: 1

Related Questions