PHP- 注意:未定义的偏移量:1
PHP- Notice: Undefined offset: 1
我有这段代码可以将数字转换成文字。我在一个网站上找到的(我不记得那个网站了)。
该代码通常运行良好,但在十位为 1 或 0 的数字中会出现此错误:
Notice: Undefined offset: 1 in C:\xampp\htdocs\...\printreceipt.php on line 103.
小数点后的数字也不正确。
我非常努力地修改代码来解决问题,但我还没有解决。
另外,我在 stakoverflow 中发现了一些标题相同的问题,但没有这样的代码,我试图从这些问题的答案中解决我的问题。
我的代码的一些结果:
154.000 = 一百五十四美元。
615.000 = 注意:未定义的偏移量:1 in .. 六百五 Dollar/s
545.152 = 五百四十五 Dollar/s 和十五已发送
这是函数代码:
function numtowords($num){
$decones = array(
'01' => "One",
'02' => "Two",
'03' => "Three",
'04' => "Four",
'05' => "Five",
'06' => "Six",
'07' => "Seven",
'08' => "Eight",
'09' => "Nine",
10 => "Ten",
11 => "Eleven",
12 => "Twelve",
13 => "Thirteen",
14 => "Fourteen",
15 => "Fifteen",
16 => "Sixteen",
17 => "Seventeen",
18 => "Eighteen",
19 => "Nineteen"
);
$ones = array(
0 => " ",
1 => "One",
2 => "Two",
3 => "Three",
4 => "Four",
5 => "Five",
6 => "Six",
7 => "Seven",
8 => "Eight",
9 => "Nine",
10 => "Ten",
11 => "Eleven",
12 => "Twelve",
13 => "Thirteen",
14 => "Fourteen",
15 => "Fifteen",
16 => "Sixteen",
17 => "Seventeen",
18 => "Eighteen",
19 => "Nineteen"
);
$tens = array(
0 => "",
2 => "Twenty",
3 => "Thirty",
4 => "Forty",
5 => "Fifty",
6 => "Sixty",
7 => "Seventy",
8 => "Eighty",
9 => "Ninety"
);
$hundreds = array(
"Hundred",
"Thousand",
"Million",
"Billion",
"Trillion",
"Quadrillion"
); //limit t quadrillion
$num = number_format($num,2,".",",");
$num_arr = explode(".",$num);
$wholenum = $num_arr[0];
$decnum = $num_arr[1];
$whole_arr = array_reverse(explode(",",$wholenum));
krsort($whole_arr);
$rettxt = "";
foreach($whole_arr as $key => $i){
if($i < 20){
$rettxt .= $ones[$i];
}
elseif($i < 100){
$rettxt .= $tens[substr($i,0,1)];
$rettxt .= " ".$ones[substr($i,1,1)];
}
else{
$rettxt .= $ones[substr($i,0,1)]." ".$hundreds[0];
$rettxt .= " ".$tens[substr($i,1,1)];
$rettxt .= " ".$ones[substr($i,2,1)];
}
if($key > 0){
$rettxt .= " ".$hundreds[$key]." ";
}
}
$rettxt = $rettxt." Dollar/s";
if($decnum > 0){
$rettxt .= " and ";
if($decnum < 20){
$rettxt .= $decones[$decnum];
}
elseif($decnum < 100){
$rettxt .= $tens[substr($decnum,0,1)];
$rettxt .= " ".$ones[substr($decnum,1,1)];
}
$rettxt = $rettxt." Sent";
}
return $rettxt;
}
十位1和0的错误在以下部分:
else{
$rettxt .= $ones[substr($i,0,1)]." ".$hundreds[0];
$rettxt .= " ".$tens[substr($i,1,1)];
$rettxt .= " ".$ones[substr($i,2,1)];
}
我认为但我不确定,小数点的错误在以下几行:
if($decnum > 0){
$rettxt .= " and ";
if($decnum < 20){
$rettxt .= $decones[$decnum];
}
elseif($decnum < 100){
$rettxt .= $tens[substr($decnum,0,1)];
$rettxt .= " ".$ones[substr($decnum,1,1)];
}
$rettxt = $rettxt." Sent";
}
foreach ($whole_arr as $key => $i) {
if ($i < 20) {
$rettxt .= $ones[$i];
} elseif ($i < 100) {
$rettxt .= $tens[substr($i, 0, 1)];
$rettxt .= " " . $ones[substr($i, 1, 1)];
} else {
$rettxt .= $ones[substr($i, 0, 1)] . " " . $hundreds[0]; //fix here
if(substr($i, 1, 1) != 1){
$rettxt .= " " . $tens[substr($i, 1, 1)];
$rettxt .= " " . $ones[substr($i, 2, 1)];
}
else{
$rettxt .= " " . $ones[substr($i, 0, 1)+9];
}
}
if ($key > 0) {
$rettxt .= " " . $hundreds[$key] . " ";
}
}
我有这段代码可以将数字转换成文字。我在一个网站上找到的(我不记得那个网站了)。 该代码通常运行良好,但在十位为 1 或 0 的数字中会出现此错误:
Notice: Undefined offset: 1 in C:\xampp\htdocs\...\printreceipt.php on line 103.
小数点后的数字也不正确。 我非常努力地修改代码来解决问题,但我还没有解决。 另外,我在 stakoverflow 中发现了一些标题相同的问题,但没有这样的代码,我试图从这些问题的答案中解决我的问题。
我的代码的一些结果:
154.000 = 一百五十四美元。
615.000 = 注意:未定义的偏移量:1 in .. 六百五 Dollar/s
545.152 = 五百四十五 Dollar/s 和十五已发送 这是函数代码:
function numtowords($num){
$decones = array(
'01' => "One",
'02' => "Two",
'03' => "Three",
'04' => "Four",
'05' => "Five",
'06' => "Six",
'07' => "Seven",
'08' => "Eight",
'09' => "Nine",
10 => "Ten",
11 => "Eleven",
12 => "Twelve",
13 => "Thirteen",
14 => "Fourteen",
15 => "Fifteen",
16 => "Sixteen",
17 => "Seventeen",
18 => "Eighteen",
19 => "Nineteen"
);
$ones = array(
0 => " ",
1 => "One",
2 => "Two",
3 => "Three",
4 => "Four",
5 => "Five",
6 => "Six",
7 => "Seven",
8 => "Eight",
9 => "Nine",
10 => "Ten",
11 => "Eleven",
12 => "Twelve",
13 => "Thirteen",
14 => "Fourteen",
15 => "Fifteen",
16 => "Sixteen",
17 => "Seventeen",
18 => "Eighteen",
19 => "Nineteen"
);
$tens = array(
0 => "",
2 => "Twenty",
3 => "Thirty",
4 => "Forty",
5 => "Fifty",
6 => "Sixty",
7 => "Seventy",
8 => "Eighty",
9 => "Ninety"
);
$hundreds = array(
"Hundred",
"Thousand",
"Million",
"Billion",
"Trillion",
"Quadrillion"
); //limit t quadrillion
$num = number_format($num,2,".",",");
$num_arr = explode(".",$num);
$wholenum = $num_arr[0];
$decnum = $num_arr[1];
$whole_arr = array_reverse(explode(",",$wholenum));
krsort($whole_arr);
$rettxt = "";
foreach($whole_arr as $key => $i){
if($i < 20){
$rettxt .= $ones[$i];
}
elseif($i < 100){
$rettxt .= $tens[substr($i,0,1)];
$rettxt .= " ".$ones[substr($i,1,1)];
}
else{
$rettxt .= $ones[substr($i,0,1)]." ".$hundreds[0];
$rettxt .= " ".$tens[substr($i,1,1)];
$rettxt .= " ".$ones[substr($i,2,1)];
}
if($key > 0){
$rettxt .= " ".$hundreds[$key]." ";
}
}
$rettxt = $rettxt." Dollar/s";
if($decnum > 0){
$rettxt .= " and ";
if($decnum < 20){
$rettxt .= $decones[$decnum];
}
elseif($decnum < 100){
$rettxt .= $tens[substr($decnum,0,1)];
$rettxt .= " ".$ones[substr($decnum,1,1)];
}
$rettxt = $rettxt." Sent";
}
return $rettxt;
}
十位1和0的错误在以下部分:
else{
$rettxt .= $ones[substr($i,0,1)]." ".$hundreds[0];
$rettxt .= " ".$tens[substr($i,1,1)];
$rettxt .= " ".$ones[substr($i,2,1)];
}
我认为但我不确定,小数点的错误在以下几行:
if($decnum > 0){
$rettxt .= " and ";
if($decnum < 20){
$rettxt .= $decones[$decnum];
}
elseif($decnum < 100){
$rettxt .= $tens[substr($decnum,0,1)];
$rettxt .= " ".$ones[substr($decnum,1,1)];
}
$rettxt = $rettxt." Sent";
}
foreach ($whole_arr as $key => $i) {
if ($i < 20) {
$rettxt .= $ones[$i];
} elseif ($i < 100) {
$rettxt .= $tens[substr($i, 0, 1)];
$rettxt .= " " . $ones[substr($i, 1, 1)];
} else {
$rettxt .= $ones[substr($i, 0, 1)] . " " . $hundreds[0]; //fix here
if(substr($i, 1, 1) != 1){
$rettxt .= " " . $tens[substr($i, 1, 1)];
$rettxt .= " " . $ones[substr($i, 2, 1)];
}
else{
$rettxt .= " " . $ones[substr($i, 0, 1)+9];
}
}
if ($key > 0) {
$rettxt .= " " . $hundreds[$key] . " ";
}
}