【问题标题】:Break in foreach with certain conditions [duplicate]在某些条件下闯入 foreach [重复]
【发布时间】:2018-06-23 22:14:10
【问题描述】:

我的学校作业有些问题。由于经验不足,我对操作数组感到困惑。我正在使用foreach 从数组中获取值,然后我想在某些条件下得到这个结果:

  1. 已经在集合变量$total_words中的单词数
  2. 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 ..................... */

【问题讨论】:

  • 幸运的是,请重新审视您放弃的问题。在您的问题中回复有关详细信息的请求。当您收到满意的答案时,接受最佳答案。当您没有收到满意的答案时,请澄清您的问题。
  • 好的,先生.. 看来,您的回答对我来说是正确的,我可以完成我的学校作业。谢谢先生!

标签: php arrays foreach break


【解决方案1】:

这是我将使用的方法——假设我理解了这个问题。检查每次迭代的字数,并根据需要截断文本或减少字数。

代码:(演示:https://3v4l.org/JFYsd

function word_limiter($sentences,$word_allowance){
    $text='';
    foreach($sentences as $sentence){
        $words=explode(' ',$sentence);
        $word_count=count($words);
        if($word_count>$word_allowance){
            $text.=implode(' ',array_slice($words,0,$word_allowance));
            // do something
            break;
        }elseif($word_count==$word_allowance){
            $text.=$sentence;
            break;
        }else{
            $text.="$sentence\n";
            $word_allowance-=$word_count;  // reduce allowance for next iteration
        }
    }
    return $text;
}

$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 ....................."
);

echo word_limiter($array,4),"\n\n";
echo word_limiter($array,12),"\n\n";
echo word_limiter($array,16),"\n\n";
echo word_limiter($array,27),"\n\n";

输出:

There are many ways

There are many ways to get to Windows 10 Advanced Startup Options.

There are many ways to get to Windows 10 Advanced Startup Options.
On many laptops, hitting

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.

【讨论】:

    猜你喜欢
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    • 2020-10-01
    • 2019-01-29
    • 2023-03-21
    • 1970-01-01
    • 2020-10-26
    • 2022-01-06
    相关资源
    最近更新 更多