【发布时间】:2018-06-23 22:14:10
【问题描述】:
我的学校作业有些问题。由于经验不足,我对操作数组感到困惑。我正在使用foreach 从数组中获取值,然后我想在某些条件下得到这个结果:
- 已经在集合变量
$total_words中的单词数 -
foreach中有条件。 如果$total words未传递数组值中的字数,打印所有数组值。但是要 else 打断所有单词,然后按最后一个单词停止单词。
更多细节你可以看到我想要的结果
$array = array(
"There are many ways to get to Windows 10 Advanced Startup Options.",
"On many laptops, hitting F11 as soon as you power on will get you there.",
"Booting off an install disk and hitting Next then Repair will do the job",
"n ....................."
);
$total_words = 4;
foreach($array as $value){
$count = explode(" ", $value);
if( count($count) <= $total_words){
echo $value. "\n\n";
} else {
//echo i do something
break;
}
}
//output $total_words = 4;
/* There are many */
//output $total_words = 12;
/* There are many ways to get to Windows 10 Advanced Startup Options. */
//output $total_words = 16;
/* There are many ways to get to Windows 10 Advanced Startup Options. */
/* On many laptops, hitting */
//output $total_words = 20000;
/* There are many ways to get to Windows 10 Advanced Startup Options. */
/* On many laptops, hitting F11 as soon as you power on will get you there. */
/* Booting off an install disk and hitting Next then Repair will do the job */
/* n ..................... */
【问题讨论】:
-
幸运的是,请重新审视您放弃的问题。在您的问题中回复有关详细信息的请求。当您收到满意的答案时,接受最佳答案。当您没有收到满意的答案时,请澄清您的问题。
-
好的,先生.. 看来,您的回答对我来说是正确的,我可以完成我的学校作业。谢谢先生!