我有一个带有单词的mysql数据库。我正在用while语句打印所有单词。所以我得到:
马铃薯番茄生菜
一切正常,但是我想按长度对单词进行排序。我试过了:
if(strlen($go['words']) == 4 ){ echo "this are the 4 letter words:"; }
但这会在每4个字母的单词前打印句子。而我希望只将其打印一次作为所有4个字母单词的标题。当然我也想用5,6,7个字母的单词来做。
我曾考虑过要进行多个sql查询,但是对于服务器来说,要处理很多访问者,那就太多了。
这是你想要的吗?
$words = array("fish", "mouse", "rabbit", "turtle", "duck");
sort($words);
$last_letter_count = 0;
foreach($words as $word)
{
if ( strlen($word) != $last_letter_count )
{
$last_letter_count = strlen($word);
echo "These are the $last_letter_count letter words:\n";
}
echo $word . "\n";
}
输出:
These are the 4 letter words:
duck
fish
These are the 5 letter words:
mouse
These are the 6 letter words:
rabbit
turtle
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句