0

I am using codeigniter geting comma separated values but not working fine. i am sharing all code like first output foreach after gating result not show comma separated values than second show array print value aftra need output comma separated values like SLR,ACCN

Model code here

public function display_coach_name($coachID='')
            {   
        $db2 = $this->load->database('rail',TRUE);
        $ids = explode(',',$coachID);
        $db2->select('coach_name');
        $db2->from('mcc_coach');
        $db2->where_in('id',$ids);
        $query = $db2->get();
        //echo $db2->last_query(); die;

        if ($query->num_rows() > 0):

        return $query->result_array();
        else:
            return 0;
        endif;
            }

output

<?php foreach ($coachname as $val){ echo $pizza = $val['coach_name']; }?>

//---------------------foreach-----------------output---

SLRACCN

array print

$coachname = $this->rail_ceil_model->display_coach_name($coachID);
echo"<pre>";
print_r($coachname);
//---------------------output----------------------
    Array
    (
        [0] => Array
            (
                [coach_name] => GS
            )

        [1] => Array
            (
                [coach_name] => SLR
            )

    )

    Array
    (
        [0] => Array
            (
                [coach_name] => GS
            )

        [1] => Array
            (
                [coach_name] => SLR
            )

    )

    Array
    (
        [0] => Array
            (
                [coach_name] => GS
            )

        [1] => Array
            (
                [coach_name] => SLR
            )

    )

    Array
    (
        [0] => Array
            (
                [coach_name] => GS
            )

        [1] => Array
            (
                [coach_name] => SLR
            )

    )

I have need output

SLR,ACCN

3 Answers3

1

You can use implode function for converting array into comma separated values

$coachname = $this->rail_ceil_model->display_coach_name($coachID);
foreach($coachname as $val){
if(is_array($val)){
   $list .= $val['coach_name'].',';        
}}

print_r(substr ( $list , 0 , strlen($list) -1 )); 

or

$coachname = $this->rail_ceil_model->display_coach_name($coachID);
print_r(implode(', ', array_map('coach_name', $coachname))); 

or

echo implode(', ', array_column($coachname, 'coach_name'));
Ankit Prajapati
  • 417
  • 1
  • 8
  • 20
  • A PHP Error was encountered Severity: Notice Message: Array to string conversion Filename: views/assign_train.php Line Number: 72 – Amitabh kumar Feb 05 '20 at 06:33
0

You may use implode function for getting comma separated values.

<?php
$coachname = array("ACCN", "SLR", "CN");
$new = implode(',',$coachname);
echo $new;
?>

Output will be:

ACCN,SLR,CN

Edit your code accordingly.

Aishwaryas
  • 633
  • 2
  • 18
  • 44
0

Please use array_map function then use implode function

$coachname_separated = array_map (function($value){
    return $value['coach_name'];
} , $coachname);

$List = implode(', ', $coachname_separated); 

echo $List;
Marmik Patel
  • 192
  • 2
  • 12