我有一个用PHP构建的用于分配作业的“您是什么角色”测验网站.我还需要存储有多少人可以得到5个可能字符中的每个字符.
我有一个仅包含文本的文本文件-0 0 0 0 0
每个0代表一个人选择特定角色的次数.
我将其爆炸成一个数组
$txt = "results2.txt";
$scores = file_get_contents($txt);
$editablescores = explode(",", $scores);
然后根据某人收到的分数,我想在数组中的适当0上加1.
这是我正在使用的示例,但不起作用.出现以下错误.注意:未定义的偏移量:第53行的/Users/sinclaa3/Sites/PHPStyles/hi.php中的4
0 0 0 0显示0,但随后在其上添加1. 0 0 0 0 0 1
if ($score < 6 ) {
$editablescores[0]++;
//0 denotes the position in the array that I want to add one to
};
$storablescores = implode(",", $editablescores);
file_put_contents($txt, $storablescores);
解决方法:
你的爆炸是错误的;您说您有0 0 0 0 0,然后尝试在.上爆炸.固定:
$editablescores = explode(" ", $scores);
请注意,由于相同的原因,您的内爆是错误的;它应该是:
$storablescores = implode(" ", $editablescores);
标签:add,storing-data,arrays,php
来源: https://codeday.me/bug/20191030/1964330.html