Home > OS >  how to remove coma between array value from API response action?
how to remove coma between array value from API response action?

Time:10-15

I have this API action in my PHP APP :

    //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 Psr\Http\Message\ResponseInterface object 
    });

I Call this API action using this script :

$.get("<?= BasePath()?>/api/getassistantrander/"  appoint_id, function(res) {
            document.getElementById("assit_array").innerHTML = res;
 }); 

Here is the out put :

[["<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>"]]

Now the Comma (,) between this two array value its show up in the rander list , I have try to add to the rsponse value

 $response = $response->withJson(implode(', ', array_filter($body))); 

But it dose not work .. I hope to find out how I can remove that comma from shown in the API result Thanx..

CodePudding user response:

Replace ],[ with ][ in javascript at front-end side.

document.getElementById("assit_array").innerHTML = res.replaceAll(/\](,)\s?\[/gi, '][')
                                      OR
document.getElementById("assit_array").innerHTML = res.replaceAll('],[', '][')
  • Related