Php - return count() in words

376 views Asked by At

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)

5

There are 5 answers

3
w00d On BEST ANSWER

If it's only a handful of number:

$number = count($yournameit);
$count_words = array( "zero", "one" , "two", "three", "four" );
echo $count_words[$number];
3
Amal Murali On

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.

1
dmid On

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

1
karmicdice On

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"

0
Joke_Sense10 On

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