只有当其中一部分是真的时,我才能用什么来使 while 语句发生


What could i use to make a while statement happen only if part of it were true

我有这个 while 语句从数组中提取信息。我只希望它在数组项目之一$history_type不等于"费用"时列出条目。

这是我到目前为止所拥有的

$i = 0;
while ($i <= 9):
    $history_date = $history[result][$i][Date];
    $history_date_format = gmdate("m-d-y", $history_date);      
    $history_type = $history[result][$i][Type];
    if($history_type == 'spent'):
        $history_type = 'Buy';
        elseif($history_type == 'earned'):
            $history_type = 'Sold';
        elseif ($history_type == 'fee'):
            $history_type = 'Fee';
        else:
            $history_type = 'Error';
    endif;
    $history_usd = $history[result][$i][Balance][value];
    $history_btc = $history[result][$i][Trade][Amount][value];
    $history_amt = $history[result][$i][Value][value];
    $history_rate = round($history_amt / $history_btc,2);
    echo '<tr><td>'.$history_type.'</td><td>'.$history_rate.'</td><td>'.$history_amt.'</td><td>'.$history_usd.'</td><td>'.$history_btc.'</td><td>'.$history_date_format.'</td></tr>';
    $i++;
endwhile;

定义 $history_type 后添加一行:

if($history_type == 'fee') continue;

continue;语句移动到当前 while 步骤的末尾。

附言。您可能希望将$i++移动到该语句的顶部,因为它也会被跳过。

替换您的:

echo '<tr><td>'.$history_type.'</td><td>'.$history_rate.'</td><td>'.$history_amt.'</td><td>'.$history_usd.'</td><td>'.$history_btc.'</td><td>'.$history_date_format.'</td></tr>';

跟:

if($history_type != 'Fee'):
  echo '<tr><td>'.$history_type.'</td><td>'.$history_rate.'</td><td>'.$history_amt.'</td><td>'.$history_usd.'</td><td>'.$history_btc.'</td><td>'.$history_date_format.'</td></tr>';
endif;

也就是说,在这个循环迭代中,如果 $history_type 不等于 'Fee',则回显 '....."