使用implode函数后,如何删除数组中的最后两个逗号

时间:2018-12-23 07:46:13

标签: php

我正在尝试使用trim函数删除数组的最后两个逗号,但无法使其正常工作。...我的语法正确吗?

在将数组内嵌到字符串后,我尝试使用trim函数

if(isset($_POST['doctors'])) {
$doctors = $_POST['doctors'];
$doctors = implode(', ', $doctors);
$doctors = strip_tags($doctors);
$doctors = trim($doctors);
} else {
  $doctors = 'Not Supplied';
}

我也使用了rtrim,但仍然无法删除最后一个逗号...我还将包括html表单,以防万一我在此做错了事:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
 <div class="medical_form">
    <div class="medical_form_title">
    <h2>Submit a Review of a Medical Clinic</h2>
 </div>
   <form class="medical_form" action="medical_review_process.php" method="POST">
    <br />
    <label>Name of Medical Clinic:</label>
    <br />
    <br />
    <input type="text" name="clinic_name" >
    <br />
    <br />
    <label>Address of the Clinic:</label>
    <br />
    <br />
    <input type="text" name="clinic_address" >
    <br />
    <br />
    <label>Phone number of the Clinic:</label>
    <br />
    <br />
    <input type="text" name="clinic_number" >
    <br />
    <br />
    <label>Are you a healthcare professional or a patient?</label>
    <br />
    <br />
    <input type="radio" name="type_of_profession" value="healthcare">Healthcare
    <br />
    <br />
    <input type="radio" name="type_of_profession" value="patient">Patient
    <br />
    <br />
    <label>Which of the following services does your clinic provide?</label>
    <br />
    <br />
    <input type="checkbox" name="services[]" value="skin_cancer">Skin cancer check-ups<br />
    <input type="checkbox" name="services[]" value="pregnancy">Pregnancy/antenatal check-ups<br>
    <input type="checkbox" name="services[]" value="pap_smears">Pap smears<br>
    <input type="checkbox" name="services[]" value="immunisations">Immunisations<br>
    <input type="checkbox" name="services[]" value="ear_syringe">Ear syringe<br>
    <input type="checkbox" name="services[]" value="ear_irrigation">Ear irrigation<br>
    <input type="checkbox" name="services[]" value="ear_suctioning">Ear suctioning<br>
    <input type="checkbox" name="services[]" value="eye_check-ups">Eye check-ups<br>
    <input type="checkbox" name="services[]" value="blood test">Blood test<br>
    <br />
    <br />
    <label>Other Services:</label>
    <br />
    <br />
    <input type="text" name="others" >
    <br />
    <br />
    <label>Recommended Doctors:</label>
    <br />
    <br />
    <input type="text" name="doctors[]" >
    <br />
    <br />
    <label>Recommended Doctors(2):</label>
    <br />
    <br />
    <input type="text" name="doctors[]" >
    <br />
    <br />
    <label>Recommended Doctors(3):</label>
    <br />
    <br />
    <input type="text" name="doctors[]" >
    <br />
    <br />
    <label>Upload pictures here to support information</label>
    <br />
    <br />
    <input type="text" name="pictures" >
    <br />
    <br />
    <button type="submit" name="submit">Register</button>
   </form>
</div>
</body>
</html>

我有3位医生输入,但用户只能输入一位,如果他们只输入一位医生,我想删除最后两个逗号:

我还有以下图像:

enter image description here

2 个答案:

答案 0 :(得分:1)

据我所知,有两种可能性。

trim()仅删除空格,因此要修剪逗号,还需要添加要修剪的字符(在此示例中为逗号和空格)...

$doctors = trim($doctors, ", ");

或者您可以在使用array_filter() ...爆破之前从数组中删除空条目。

$doctors = array_filter($_POST['doctors']);

答案 1 :(得分:1)

您可以尝试

rtrim(',',$doctors);