Php - return count()in words [英] Php - return count() in words
问题描述
我的代码中有以下内容:
I have the following in my code:
$my_count = count($total_elements[$array_object]);
$ my_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, ...)
在此特定情况下, :
$numbers = array(
2 => 'two',
3 => 'three',
4 => 'four',
5 => 'five',
6 => 'six',
);
如何获取给定数组中的元素数量,然后 echo
相应的自然数(人类可读数)从数组?或者更好 - 有更好的方法吗?
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)
现在,当然可以用 switch()
:
switch ($my_count) {
case 0:
echo "zero";
break;
case 1:
echo "one";
break;
case 2:
echo "two";
break;
// etc...
}
优雅的我。
我相信有一个更优雅的方式实现,即使现在我只有5个数字,我想在其他情况下重新使用一些功能。
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 .
(对不起,如果这是一个愚蠢的问题,但在SE或Google上搜索与关键字 PHP
- 字
和
(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)
推荐答案
如果只有少数几个:
$number = count($yournameit);
$count_words = array( "zero", "one" , "two", "three", "four" );
echo $count_words[$number];
这篇关于Php - return count()in words的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!