如何使用代码点火器显示逗号分隔的值



我正在使用代码点火器获取逗号分隔值,但无法正常工作。 我正在共享所有代码,例如门控结果后的第一个输出 不显示逗号分隔值比第二个显示数组打印值 aftra 需要输出逗号分隔值,如SLR、ACCN

此处为模型代码

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;
}

输出

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

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

SLRACCN

阵列打印

$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
)
)

我需要输出

单反,中联

您可以使用内爆函数将数组转换为逗号分隔值

$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 )); 

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

echo implode(', ', array_column($coachname, 'coach_name'));

您可以使用内爆函数来获取逗号分隔的值。

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

输出将是:

ACCN,SLR,CN

相应地编辑代码。

请使用array_map函数,然后使用内爆函数

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

相关内容

  • 没有找到相关文章

最新更新