php - 未定义的偏移错误,构建数组的新功能

标签 php arrays

我正在学习数组并将其放在一起......它的工作原理意味着我得到了我想要的结果,但我得到了未定义的偏移错误。

$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/

相关文章:

javascript - Symfony 和 React 路由器,找不到路由

php - 如何使用 PHP 解密对称加密的 OpenPGP 消息?

javascript - jQuery "Meet the team"效果

java - 将java类字段直接转换为字节数组

python - 将元胞数组(.mat 文件)加载到 Python 中

.net - 替换 for...if 数组迭代

php - 如何使从数据库中抓取的链接显示为可点击的事件链接?

php - 在 MySQL 中存储日期和小时(不包括分钟和秒)

javascript - 无法使用 Angular.js 和 PHP 获取文件参数值

PHP DATE逻辑问题