这是在回答您删除错误,所以它只是显示5.
我认真没有理解对你的意图与此代码,但摆脱你的错误,你需要重构的东西。
您的值$u
取决于$txt
的长度以及您在str_split中设置的位数。 $ u的值太大,因为索引6和7的条目不存在于您的数组中。
1日去...
<?php
$txt = "12345678910"; // The string of digits
$nu = 2; // How many digits per array
$disp = str_split($txt, $nu); // The array of strings of $nu digits
$u = count($disp); // Calculate the size of $disp - don't guess
$count = 0; // initialise the variable
for ($i = 0; $i < $u; $i++) {
$count += count($disp[$i]); // count($disp[$i]) will always be 1
// Looking for an entry that isn't $nu digits in size
if (strlen($disp[$i]) != $nu) {
$count = substr($count, 0, 1); // Rip off the 1st digit of the index count
$uu = ($count - 1); // decrement it?
echo $uu; // display it
}
}
另一种方式...... 因此,使用的foreach是更好,因为它需要的所有的辛勤工作照顾。
<?php
$txt = "12345678910";
$nu = 2;
$disp = str_split($txt, $nu);
// DEBUG
var_dump($disp);
$count = 0; // Initialise the variable before we can += it
foreach ($disp as $number) {
$count += 1;
if (strlen($number) != $nu) {
// Taking the index count, getting the 1st digit
$count = substr($count, 0, 1);
// Then subtracting 1 from it
$uu = $count - 1;
echo $uu;
}
}
所以这只是解决你有什么,所以它有点更好,没有错误只。
在PHP中,从字符串到整数并且再次返回时,盲目类型化可能是一个陷阱。
我只想得到单个结果示例5而不是55555 –