php:如何从逗号(,)分隔的数组中获得单值
问题描述:
我已经在我的项目中应用了标记功能,就像堆栈溢出一样。php:如何从逗号(,)分隔的数组中获得单值
当我subit多个标签从我的形式,并在我的控制器 我得到这样的:
$this->input->post('tags_id')
所以我得到2,3,44,442
为$this->input->post('tags_id')
完全值。
我想要的是我希望将每个值一个一个地插入到我的标签表中。
我怎样才能从逗号分隔列表中的每个值插入到我的分贝? 请帮我...
我做下面插入到我的分贝
function entry_insert_instdetails()
{
$this->load->database();
$this->db->trans_begin();
$data=array('instrunction'=> $this->input->post('tags_id'),
'creater_id'=>$this->session->userdata('userid'),
);
$this->db->insert('instructions',$data);
if ($this->db->trans_status() === FALSE)
{
$this->db->trans_rollback();
}
else
{
$this->db->trans_commit();
}
}
答
您可以用PHP的爆炸功能
$data = array('instrunction'=> $this->input->post('tags_id');
$ids = explode(',',$data);
unset($data);
foreach($ids as $id)
{
$data['tag_id'] = $id; // tag_id is table column
$this->db->insert('instructions',$data);
unset($data);
}
检查很容易做到这一点[爆炸]( http://www.php.net/explode)函数。将字符串拆分成数组。 [str_getcsv](http://php.net/str_getcsv)对具有一些特殊功能的逗号分隔值(csv)也是这样。 – Imperative 2013-05-08 06:20:15