需要对 foreach() 的结果进行分组

foreach ($mgrAccounts as $mgrAccount) {

    $mgrAccNames  = $mgrAccount['account_name'];

    $userAccNames = $userRow['account_name'];

    $userAccNames = explode(',', str_replace(['{', '}', '"'], "", $userAccNames));

    $mgrAccNames  = explode(',', str_replace(['{', '}', '"'], "", $mgrAccNames));

    $accounts     = array_intersect($userAccNames, $mgrAccNames);


    if (!empty($accounts)) {

        $tableStr .= "<tr>";

        $tableStr .= "<td><select name='selectAccounts' id='selectAccounts'>";

        foreach ($accounts as $account) {

            $tableStr .= "<option>{$account}</option>";

        }

        $tableStr .= "</select></td>";

        $tableStr .= "<td><a href='/reset_password.php'>Password Reset</a></td>";

        $tableStr .= "</tr>";

    }

}

我需要能够将每个帐户的下拉列表作为其中的一个选项。目前,它正在为每个帐户创建一个新选项。如何将所有内容组合在一起以放入选项下拉列表?


红颜莎娜
浏览 237回答 1
1回答

犯罪嫌疑人X

只需在循环外打开和关闭您的桌子$tableStr .= "<tr>";$tableStr .= "<td><select name='selectAccounts' id='selectAccounts'>";foreach ($mgrAccounts as $mgrAccount) {&nbsp; &nbsp; $mgrAccNames = $mgrAccount['account_name'];&nbsp; &nbsp; $userAccNames = $userRow['account_name'];&nbsp; &nbsp; $userAccNames = explode(',', str_replace(['{', '}', '"'], "", $userAccNames));&nbsp; &nbsp; $mgrAccNames = explode(',', str_replace(['{', '}', '"'], "", $mgrAccNames));&nbsp; &nbsp; $accounts = array_intersect($userAccNames, $mgrAccNames);&nbsp; &nbsp; if (!empty($accounts)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;foreach ($accounts as $account) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$tableStr .= "<option>{$account}</option>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;}}$tableStr .= "</select></td>";$tableStr .= "<td><a href='/reset_password.php'>Password Reset</a>&nbsp;</td>";$tableStr .= "</tr>";
打开App,查看更多内容
随时随地看视频慕课网APP