我想在数组返回值中添加逗号。
我的代码是
$to_ids_string = "";
$to_id = $this->input->post('to');
for ($r = 0; $r < count($this->input->post('to')); $r++) {
if ($to_ids_string != "") {
$to_ids_string = $to_ids_string . "," . $to_id[$r];
} else {
$to_ids_string = $to_id[$r];
}
}
$a = explode(',', $to_ids_string);
foreach ($a as $item) {
$user_id = $item;//echo $user_id;
$email_id['to_email'] = $this->admin_model-
>get_email_id($user_id);
$b = implode(',',$email_id[0]['to_email']);
echo $b;
}exit;
当我打印$ email_id [&#39; to_email&#39;]时,它会返回 数组([0] =&gt;数组([email] =&gt; dipti@gmail.com))数组([0] =&gt;数组([email] =&gt; abc@gmail.com))但我想要以逗号分隔的返回邮件。我使用上面的implode函数,但它返回错误,如Message:Array to string conversion
答案 0 :(得分:0)
现在只有我注意到它在foreach里面。所以每次只收到一封邮件。因此你需要像这样连接.declare foreach之外的变量作为空变量,如$b.='';
然后在foreach内部连接像$b.=$email_id['to_email'][0]['email'].',';
这样的foreach之后你需要删除字符串末尾的附加逗号{ {1}}
rtrim()