我在我的PHP APP中有这个API动作:
//get api assistant rander
$app->get('/getassistantrander/{id}', function ($request, $response, $args) {
$id = $args["id"] ?? null; // Get the input value
if ($id !== null) {
$fetchassitname = Conn()->executeQuery("SELECT * FROM `users` WHERE `user_tybe` = '".AdjustSql('1')."'")->fetchAll();
$body = array();
$checkid = 0;
foreach($fetchassitname as $row){
$assitant_id = $row['id'];
$assistant_name = $row['username'];
$checkassistant_list = ExecuteScalar("SELECT COUNT(*) FROM `assistants_list` WHERE `appoint_id` = '".AdjustSql($id)."' AND `assistant_id` = '".AdjustSql($assitant_id)."'");
if($checkassistant_list == '0'){
$body[] = array("<div class='card ew-card card-body'><div class='form-check'><input class='form-check-input' type='checkbox' id=".$checkid." name='type-$id' value=".$assitant_id." /><label class='form-check-label' for=".$checkid.">".$assistant_name."</lebel></div></div>");
$checkid++ ;
}
}
if($checkid == 0){
$Language = Container("language");
$empty_list_mesg = $Language->phrase("emptyassistanlist");
$body = "<div class='swal2-validation-message my-validation-message' style='display: flex; color: #F11F01;'>$empty_list_mesg</div>";
}
$response = $response->withJson($body); // Write to response
}
return $response; // Return PsrHttpMessageResponseInterface object
});
使用下面的脚本调用这个API动作:
$.get("<?= BasePath()?>/api/getassistantrander/"+ appoint_id, function(res) {
document.getElementById("assit_array").innerHTML = res;
});
输出:
[["<div class='card ew-card card-body'><div class='form-check'><input class='form-check-input' type='checkbox' id=0 name='type-166' value=1 /><label class='form-check-label' for=0>Assistan User</lebel></div></div>"],["<div class='card ew-card card-body'><div class='form-check'><input class='form-check-input' type='checkbox' id=1 name='type-166' value=6 /><label class='form-check-label' for=1>Assistant User 1</lebel></div></div>"]]
现在,这两个数组值之间的逗号(,)将显示在排序列表中,我尝试添加到响应值
$response = $response->withJson(implode(', ', array_filter($body)));
但它不工作…我希望找出如何从API结果中删除逗号谢谢. .
将javascript前端的],[
替换为][
document.getElementById("assit_array").innerHTML = res.replaceAll(/](,)s?[/gi, '][')
OR
document.getElementById("assit_array").innerHTML = res.replaceAll('],[', '][')