我正在学习数组并将其放在一起......它的工作原理意味着我得到了我想要的结果,但我得到了未定义的偏移错误。
$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++; }
这里是错误:
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
我知道有一种更干净的方法来获取错误消息并将它们分配给变量并在它们周围包装一些 css...但正如我所说的我正在学习。
最佳答案
要正确迭代 $err_array
,您应该使用 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];
}
}
关于php - 未定义的偏移错误,构建数组的新功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10859731/