php 编程新手,也见过类似的问题
因此,目前我正在尝试修复游戏统计服务器的一些旧代码(这肯定已经过时,并且最近已被新版本替换)。大部分代码已被弃用,因此正确修复它是一种猜测,但我发现一个部分完全损坏,我不知道如何修复,并且它导致了致命错误。
代码如下
for ($i=0; $i<$armyCount; $i++)
{
$summary['total']['time'] += $armies[0]['time'.$i];
$summary['total']['win'] += $armies[0]['win'.$i];
$summary['total']['loss'] += $armies[0]['loss'.$i];
$summary['total']['score'] += $armies[0]['score'.$i];
$summary['total']['best'] += $armies[0]['best'.$i];
$summary['total']['worst'] += $armies[0]['worst'.$i];
$summary['total']['brnd'] += $armies[0]['brnd'.$i];
}
我得到的错误如下
Notice: Uninitialized string offset: 0 in C:\xampp\htdocs\dontneedthis\playerstats.inc.php on line 136
Fatal error: Cannot use string offset as an array in C:\xampp\htdocs\dontneedthis\playerstats.inc.php on line 136
我见过类似的问题,并看到它们是如何解决的,但我不完全理解它是如何完成的,所以不太知道如何修复这个。任何帮助都会很棒,我完全打算向剩下的社区发布已修复错误且完全工作的代码(每当我完成该代码时)。
So currently I'm trying to fix up some old code for a stats server for a game (that is definitely outdated, and has been replaced recently with a new version). MUCH of the code is deprecated, so it's kind of guesswork on fixing it right, but I found a section thats completely broken that I have no idea how to fix, and it's causing a fatal error.
The code is as follows
for ($i=0; $i<$armyCount; $i++)
{
$summary['total']['time'] += $armies[0]['time'.$i];
$summary['total']['win'] += $armies[0]['win'.$i];
$summary['total']['loss'] += $armies[0]['loss'.$i];
$summary['total']['score'] += $armies[0]['score'.$i];
$summary['total']['best'] += $armies[0]['best'.$i];
$summary['total']['worst'] += $armies[0]['worst'.$i];
$summary['total']['brnd'] += $armies[0]['brnd'.$i];
}
The errors I get are as follows
Notice: Uninitialized string offset: 0 in C:\xampp\htdocs\dontneedthis\playerstats.inc.php on line 136
Fatal error: Cannot use string offset as an array in C:\xampp\htdocs\dontneedthis\playerstats.inc.php on line 136
I've seen similar questions asked, and seen how they were resolved, but I don't fully understand how it was done, so don't quite know how to go about fixing this one. Any assistance would be awesome, and I fully intend to release the bugfixed and fully working code (whenever I get that done) to the community that remains.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来你的 $armies[0]['time'.$i] 被初始化为空字符串而不是数组(可能 $armies= "" )。
这意味着 php 尝试将字符串变量 $armies 作为数组进行访问。如果字符串非空,那么这将导致从该字符串中获取单个字母。但 $armies 似乎是一个空字符串,因此不可能获取索引为 0 的字母。
意味着上面的操作结果(显示了通知)不能作为数组访问。请记住,$armies 是一个字符串,并且访问该字符串的第一个字母,并且此尝试的结果将作为数组访问。
尝试解决 $armies 变量的填充方式以及填充方式错误的原因。
It looks like your $armies[0]['time'.$i] is initialized as an empty string and not as an array (likely $armies= "").
That means that php tries to access your string-variable $armies as an array. If the string is non-empty then this would result in getting single letters from that string. But it seems that $armies is an empty string and so getting the letter with index 0 is not possible.
means that the result of the operation above (which showed the notice) cannot be accessed as an array. Remember that $armies is a astring and the first letter of the string was accessed and the result of this attempt will be accessed as an array.
Try resolving how the $armies variable is filled and why its filled the wrong way.
$armies
是一个空字符串,并且您以不可恢复的方式将其视为任何数组。向我们展示
$armies
应该是什么样子,var_dump($armies);
时会得到什么,以及分配它的代码,我们将为您提供帮助弄清楚出了什么问题。$armies
is an empty string, and you are treating it as any array, in an unrecoverable way.Show us what
$armies
should look like, what you get when youvar_dump($armies);
, and the code from where you assigned it, and we'll help you work out what went wrong.