Convert array to implode value using ci

I have been getting value array and convert implode value comma separate ?

Model:

$result = $db2->query('SELECT tests FROM dpr_save_labtest where appointment_id = "8618204"');
            $data = $result->result_array();
            $res = implode(',', $data); 
            return $data;

Controller:

public function investigation_print_data()
    {
        $data = $this->doctor_health_model->investigation_print_data();
        echo json_encode($data);
        

    }

Array value:

Array
(
    [0] => Array
        (
            [tests] => COMPLETE HAEMOGRAM
        )

    [1] => Array
        (
            [tests] => CRP QUANTITATIVE
        )

)

View json print value:

'+res2[0]['tests']+'

Result:

RHEUMATOID FACTOR

I need this value:

RHEUMATOID FACTOR,CREATININE

Answer

When there is only one field in the SELECT, an implode does nothing. You need to collect the data and then return the imploded data.

$result = $db2->query('SELECT tests FROM dpr_save_labtest where appointment_id = "8618204"');
$tests = [];
while($data = $result->result_array()) {
    $tests[] = $data['tests'];
}
return implode(',', $tests);

Leave a Reply

Your email address will not be published. Required fields are marked *