3

I have the following in my code:

 $my_count = count($total_elements[$array_object]);

$my_count now contains the number of elements in $total_elements[$array_object]. I want to convert this number into its corresponding natural number (zero, one, two, ...)

In this specific case , I only have 5 numbers:

$numbers  = array(
    2  => 'two',
    3  => 'three',
    4  => 'four',
    5  => 'five',
    6  => 'six',
);

How to retrieve the number of elements in a given array and then echo the corresponding natural number (human readable number) from the array? Or better yet - is there a better way of doing this?

(I have found some functions or classes to do just that - but now those are way too bloated for the simple case I need now)

Now, of course I can do this with switch():

switch ($my_count) {
    case 0:
        echo "zero";
        break;
    case 1:
        echo "one";
        break;
    case 2:
        echo "two";
        break;
      // etc...
}

But it just looks so not elegant to me. And also quite stupid if one have more than 10 numbers.

I am sure that there is a more elegant way of achieving that, and even though now I have only 5 numbers, I would like to have some function to re-use in other cases .

(I am sorry if this is a stupid question - but - searching here on SE or Google with keywords PHP - words and count() I only found answers related to counting words in string)

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
Obmerk Kronen
  • 15,619
  • 16
  • 66
  • 105

5 Answers5

6

Pear has a packge for number words. Check this out pear Number_word

$numbers = new Number_Words();
echo $number->toWords(200);

It will help you

EDIT

You can use NumberFormatter class in php.

$f = new NumberFormatter("en", NumberFormatter::SPELLOUT);  
echo $f->format(200);

It will output "Two Hundred"

karmicdice
  • 1,063
  • 9
  • 38
5

If you only need to retrieve 5 numbers, the current approach is fine. Create an associative array containing the numbers as keys and corresponding word as values:

$numberArray  = array(
    0  => 'zero',
    1  => 'one',
    2  => 'two',
    3  => 'three',
    4  => 'four',
    5  => 'five',
    6  => 'six',
    // ...
);

Now, to retrieve the word corresponding to $my_count:

$my_count = 2;
echo $numberArray[$my_count]; // => two

You could optionally check if the index is defined using isset() before trying to echo it. This approach will work for numbers upto 10. However, this obviously won't work for larger numbers as it requires more complex rules. It isn't easy to create a function that does this in 10 or 20 lines of code. I suggest you use an existing solution instead of trying to create one from scratch. Take a look at the Number_Words PEAR class.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • I think you missunderstood .. I do want the `count()` but I want to output the result in a human readable manner – Obmerk Kronen Dec 01 '13 at 05:51
  • No - And it is stated in the original question – Obmerk Kronen Dec 01 '13 at 05:52
  • Thanks - It is in fact IMHO more elegant , and I have already adapted @w00d answer to work with my ( associative ) array .But Since I already used `w00d` answer, I would not want to un-accept it . I do however appreciate the help of the good people here :-) . And these answers are always left for future reference ( which is all what SE is about ). Thanks again – Obmerk Kronen Dec 01 '13 at 08:03
4

If it's only a handful of number:

$number = count($yournameit);
$count_words = array( "zero", "one" , "two", "three", "four" );
echo $count_words[$number];
w00d
  • 5,416
  • 12
  • 53
  • 85
  • This looks like how the OP already presents it, with shifted indices - although it's missing "five" and "six". (I've still up-voted because it shows *how* to use said look-up.) – user2864740 Dec 01 '13 at 05:56
  • OP said he did not know how to use.. so here it is – w00d Dec 01 '13 at 05:59
  • This is so simple , I am embarrassed - I guess I am tired ( still , 3 down votes for this question is strange ). – Obmerk Kronen Dec 01 '13 at 06:03
3

Theres no quick and easy way to achieve this with an inbuilt php function, however you can do it with this pear package: http://pear.php.net/package/Numbers_Words

dmid
  • 483
  • 4
  • 18
2

The PEAR Numbers_Words package provides methods for spelling numerals in words.

Reference:

PEAR: http://pear.php.net/package/Numbers_Words

PECL: http://php.net/manual/en/numberformatter.format.php

Example:

echo number_to_word( '2281941596' );

//Sample output - number_to_word
Two Billion, Two Hundreds Eighty One Million, Nine Hundreds Forty One Thousand and Five Hundreds Ninety Six

//Sample output - PEAR Class
 two billion two hundred eighty-one million nine hundred forty-one thousand five hundred ninety-six
Joke_Sense10
  • 5,341
  • 2
  • 18
  • 22