PHP使用continue

时间:2018-07-05 05:10:53

标签: php

我试图弄清楚如何在满足特定参数的情况下插入值,然后继续在循环中打印该值,我正在查看PHP的continue语句并找到了此示例。

for ($i = 0; $i < 5; ++$i) {
    if ($i == 2)
     echo "Checkpoint";
     echo $i;
        continue;
    print "$i\n";
}

这有效!作为我想要实现的,输出如下。

  

01Checkpoint234

现在,我尝试在脚本上重新创建,但无法使其正常工作。

 <?php foreach($v as $key => $value) {?>


    <?php  if ($key < 50) ?>
       {
        "key": <?= $key;?>,
        "Volume": <?= $value;?>,
        "expenses": 0,
        "color": 'green',
        "lineColor": 'green'
       } ,

       <?php continue;?>

       {
        "key": 50,
        "Volume": 0,
        "expenses": 0,
        "color": 'green',
        "lineColor": 'green'
       } ,

       {
        "key": <?= $key;?>,
        "Volume": <?= $value;?>,
        "expenses": 0,
        "color": 'green',
        "lineColor": 'green'
       } ,


    <?php }?>

我的输出低于

   {
    "key": 50,
    "Volume": 0,
    "expenses": 0,
    "color": 'green',
    "lineColor": 'green'

  } ,
    {
    "key": 39,
    "Volume": 4,
    "expenses": 1.7668,
    "color": 'green',
    "lineColor": 'green',

    } ,

    {
    "key": 50,
    "Volume": 0,
    "expenses": 0,
    "color": 'green',
    "lineColor": 'green'  
   } ,
     {
    "key": 38,
    "Volume": 5,
    "expenses": 1.739,
    "color": 'green',
    "lineColor": 'green',

    } ,

我想做的是当键(<)小于50时,我想插入一个“键”为50的元素,然后将继续打印小于50的键,任何建议都很好

1 个答案:

答案 0 :(得分:0)

您不需要继续执行此操作。 Continue将在下一次迭代时重新开始循环,并跳过之后的所有代码。此代码将打印所有项目,但是如果键小于50,它将创建一个额外的项目,键为50。这是您想要做的吗?

import * as sha512 from 'js-sha512';