我需要在php foreach中用字母编号。
foreach($array as $key=>$value){
echo 'Key is : '.$key.'<br>';
}
以上编码看起来像这样。
Key is : 1
Key is : 2
Key is : 3 ...etc
但我需要这样。
Key is : one
Key is : two
Key is : three ...etc
提前感谢
答案 0 :(得分:3)
您可以使用梨包来实现此目的:
http://pear.php.net/package-info.php?package=Numbers_Words
$numberToWord = new Numbers_Words();
echo $numberToWords->toWords(200);
答案 1 :(得分:1)
pear有一个包Numbers_Words:
$numberToWord = new Numbers_Words();
echo $numberToWords->toWords(200);
答案 2 :(得分:0)
如果你的要求像你想象的那样简单,将正整数键显示为数字,这是一个简单的解决方案:
<?php
global $level_0, $level_1, $level_2, $level_3;
$level_0=array('','one','two','three','four','five','six','seven',
'eight','nine','ten','eleven','twelve','thirteen','fourteen',
'fifteen','sixteen','seventeen','eighteen','nineteen');
$level_1=array(2=>'twenty',3=>'thirty',4=>'forty',5=>'fifty',
6=>'sixty',7=>'seventy',8=>'eighty',9=>'ninety');
$level_2='hundred';
$level_3='thousand';
function gn($num){
global $level_0, $level_1, $level_2, $level_3;
$end='';
$arrnum=array_reverse(str_split(strval($num)));
$start='';
if($num%100>0)$end=f99($num);
for($i=strlen(strval($num))-1; $i>1; $i--){
if($i==4) $start.=f99($arrnum[$i--].$arrnum[$i]).${'level_'.$i};
else {
$break=false;
if($num%pow(10,$i)==0) $break=true;
if($level_0[$arrnum[$i]]){
$start.=$level_0[$arrnum[$i]].${'level_'.$i};
}
if($break) break;
}
}
return $start.$end;
}
function f99($num){
$arrnum=array_reverse(str_split(strval($num)));
global $level_0, $level_1;
$num=$num%100; //ensure 2 digits integer
$end='';
if($num%100<20)$end=$level_0[$num%100];
else {
$end=$level_1[$arrnum[1]];
if($num%10>0) $end.=$level_0[$arrnum[0]];
}
return $end;
}
// Test statements
echo "12 - ".gn(12)." \n";
echo "20 - ".gn(20)." \n";
echo "37 - ".gn(37)." \n";
echo "50 - ".gn(50)." \n";
echo "99 - ".gn(99)." \n";
echo "125 - ".gn(125)." \n";
echo "160 - ".gn(160)." \n";
echo "300 - ".gn(300)." \n";
echo "2365 - ".gn(2365)." \n";
echo "1000 - ".gn(1000)." \n";
echo "1300 - ".gn(1300)." \n";
echo "10000 - ".gn(10000)." \n";
echo "30000 - ".gn(30000)." \n";
echo "23653 - ".gn(23653)." \n";
如果您需要更高的数字,可以在gn-function中包含新的等级。
答案 3 :(得分:0)