php 什么原因导致:“注意:未初始化的字符串偏移量”出现?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1263636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What causes: "Notice: Uninitialized string offset" to appear?
提问by Tomasz Iniewicz
I have a form that users fill out, and on the form there are multiple identical fields, like "project name", "project date", "catagory", etc. Based on how many forms a user is submitting, my goal is to:
我有一个用户填写的表单,表单上有多个相同的字段,例如“项目名称”、“项目日期”、“类别”等。根据用户提交的表单数量,我的目标是:
- loop over the number of forms
- create individual SQL insert statements
- 循环表单数
- 创建单独的 SQL 插入语句
However, PHP throws me a NOTICE that I don't seem to understand:
但是,PHP 向我抛出了一个我似乎不明白的通知:
Notice:
注意:
Notice: Uninitialized string offset: 1 ...dataPasser.php on line 90
注意:未初始化的字符串偏移量:1 ...dataPasser.php 第 90 行
PHP
PHP
$myQuery = array();
if ($varsCount != 0)
{
for ($i=0; $i <= $varsCount; $i++)
{
$var = "insert into projectData values ('" . $catagory[$i] . "', '" . $task[$i] . "', '" . $fullText[$i] . "', '" . $dueDate[$i] . "', null, '" . $empId[$i] ."')";
array_push($myQuery, $var);
}
}
There are references to this issue I am having, but they are not exact and I am having trouble deducing where the actual problem stems from. I would greatly appreciate any help in understanding what is causing the array to not initialize properly.
我遇到了对这个问题的引用,但它们并不准确,我无法推断出实际问题的根源。我将不胜感激任何帮助了解导致数组无法正确初始化的原因。
采纳答案by zombat
This error would occur if any of the following variables were actually strings or null instead of arrays, in which case accessing them with an array syntax $var[$i]would be like trying to access a specific character in a string:
如果以下任何变量实际上是字符串或 null 而不是数组,则会发生此错误,在这种情况下,使用数组语法访问它们$var[$i]就像尝试访问字符串中的特定字符:
$catagory
$task
$fullText
$dueDate
$empId
In short, everything in your insert query.
简而言之,插入查询中的所有内容。
Perhaps the $catagoryvariable is misspelled?
也许$catagory变量拼写错误?
回答by cletus
It means one of your arrays isn't actually an array.
这意味着您的数组之一实际上不是数组。
By the way, your if check is unnecessary. If $varsCount is 0 the for loop won't execute anyway.
顺便说一句,您的 if 检查是不必要的。如果 $varsCount 为 0 for 循环无论如何都不会执行。
回答by argentum47
The error may occur when the number of times you iterate the array is greater than the actual size of the array. for example:
当您迭代数组的次数大于数组的实际大小时,可能会出现该错误。例如:
$one="909";
for($i=0;$i<10;$i++)
echo ' '.$one[$i];
will show the error. first case u can take the mod of i.. for example
将显示错误。第一种情况,您可以采用 i.. 的模式,例如
function mod($i,$length){
$m = $i % $size;
if ($m > $size)
mod($m,$size)
return $m;
}
for($i=0;$i<10;$i++)
{
$k=mod($i,3);
echo ' '.$one[$k];
}
or might be it not an array (maybe it was a value and you tried to access it like an array) for example:
或者它可能不是一个数组(也许它是一个值并且您试图像数组一样访问它)例如:
$k = 2;
$k[0];
回答by Kiki974
Try to test and initialize your arrays before you use them :
在使用数组之前尝试测试和初始化它们:
if( !isset($catagory[$i]) ) $catagory[$i] = '' ;
if( !isset($task[$i]) ) $task[$i] = '' ;
if( !isset($fullText[$i]) ) $fullText[$i] = '' ;
if( !isset($dueDate[$i]) ) $dueDate[$i] = '' ;
if( !isset($empId[$i]) ) $empId[$i] = '' ;
If $catagory[$i]doesn't exist, you create (Uninitialized) one ... that's all ;
=> PHP try to read on your table in the address $i, but at this address, there's nothing, this address doesn't exist => PHP return you a notice, and it put nothing to you string.
So you code is not very clean, it takes you some resources that down you server's performance (just a very little).
如果$catagory[$i]不存在,则创建(未初始化)一个……仅此而已;=> PHP 尝试读取您的表中的地址$i,但在此地址中,什么也没有,此地址不存在 => PHP 会向您返回一条通知,并且它不会向您发送任何字符串。所以你的代码不是很干净,它占用了你一些资源,降低了服务器的性能(只是很少)。
Take care about your MySQL tables default values
注意你的 MySQL 表默认值
if( !isset($dueDate[$i]) ) $dueDate[$i] = '0000-00-00 00:00:00' ;
or
或者
if( !isset($dueDate[$i]) ) $dueDate[$i] = 'NULL' ;
回答by zbee
Check out the contents of your array with
使用
echo '<pre>' . print_r( $arr, TRUE ) . '</pre>';

