0

I need number in letters in php foreach.

foreach($array as $key=>$value){
 echo 'Key is : '.$key.'<br>';
}

The above coding looking like this.

Key is : 1
Key is : 2
Key is : 3 ...etc

But i need like this.

Key is : one
Key is : two
Key is : three ...etc

Thank in advance

Simbu
  • 27
  • 5
  • How is the array generated? – Qirel Feb 03 '16 at 13:30
  • This might help http://stackoverflow.com/questions/2112571/converting-a-number-1-2-3-to-a-string-one-two-three-in-php – Phiter Feb 03 '16 at 13:34
  • 2
    Possible duplicate of [Is there an easy way to convert a number to a word in PHP?](http://stackoverflow.com/questions/277569/is-there-an-easy-way-to-convert-a-number-to-a-word-in-php) – Abdulla Nilam Feb 03 '16 at 13:35

4 Answers4

3

There exists a pear package you can use to achive this:

http://pear.php.net/package-info.php?package=Numbers_Words

$numberToWord = new Numbers_Words();
echo $numberToWords->toWords(200);
Martin
  • 575
  • 6
  • 13
1

pear has a package Numbers_Words:

$numberToWord = new Numbers_Words();
echo $numberToWords->toWords(200);
Domain
  • 11,562
  • 3
  • 23
  • 44
0

If your requirements are as simple as you seem to state, displaying positive integer keys as numbers, here's a simple solution:

<?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";

If you need higher numbers, you can include new levels in the gn-function.

jonasfh
  • 4,151
  • 2
  • 21
  • 37
0

Here's from the clouds.

http://www.phpro.org/examples/Convert-Numbers-to-Words.html

You can put this into your library.

duduwe
  • 888
  • 7
  • 16