如何在php中用字符串连接Foreach循环

2022-08-31 00:00:00 concatenation foreach php

如何在PHP中将Foreach循环与字符串连接在一起

$traning [] = '<tr>
    <td style="width:30%" id="trainingCode_' . $key + 1 . '">
    <select class="select2 form-control" data-live-search= "true" name="trainingCode[]">
        <option value="0">SELECT CODE</option>'.
        foreach ($code=0 as $row) {
            //
        }
    .'</select>
    </td>
   </tr>';

解决方案

将代码拆分为每个人都能理解的部分:

$trContent = '<tr>
    <td style="width:30%" id="trainingCode_' . $key + 1 . '">
    <select class="select2 form-control" data-live-search= "true" name="trainingCode[]">
        <option value="0">SELECT CODE</option>';

// iterate over array and append to `$trContent`
foreach ($code as $row) {
    $trContent .= '<option></option>';
}

$trContent .= '</select>
    </td>
   </tr>';

$traning [] = $trContent;

相关文章