如何将PHP数组值指定为逗号分隔的字符串

时间:2015-05-05 04:38:38

标签: php mysql codeigniter

我有两个mysql表,sla和department,我正在创建SLA,而在创建sla时,有多个部门的选择,

当用户提交SLA表单时,部门表中受尊重的部门会获得sla id,

但是在添加之后我需要通过更新部门的连接来添加SLA,

以下是我的控制器代码,

if ($this->form_validation->run() !== FALSE) {
    $result = $this->model_admin->updatesla($sla_data); // Add SLA
    if (!$result) {
        $lastentry = $this->model_admin->LastEntrysla(); //Get last added SLA
        foreach($this->input->post('sla_department') as $department_id) {
            $sladepartment_data = array( 'sla_id' => $lastentry->sla_id);
            $this->model_admin->updatedepartmentsla($department_id,$sladepartment_data);//Update SLA to respective multiselected department
        }
        //Now getadded SLA with join of department
        foreach($this->model_admin->getaddedsla($lastentry->sla_id) as $returnsla) {
            $returnslajson = array(  'sla_id' => $returnsla->sla_id,
                'sla_name' => $returnsla->sla_name,
                'sla_days' => $returnsla->sla_days.'D '.$returnsla->sla_hours.'H '.$returnsla->sla_minutes.'M',
                'department_name' => $returnsla->department_name); //Here this SLA is assign to department X and Y, i want department_name = X, y
        }
        echo json_encode($returnslajson);
    }
}

我在每一行添加了一些评论,

当SLA添加时,我只在输出中获得1个部门,

department_name: "X"
sla_days: "1D 1H 1M"
sla_id: "38"
sla_name: "qweqweq"

如何将department_name设为X,Y

下面是数组的输出,$ this-> model_admin-> getaddedsla($ lastentry-> sla_id)

Array ( [0] => stdClass Object ( [sla_id] => 38 [sla_name] => qweqweq [sla_days] => 1 [sla_hours] => 1 [sla_minutes] => 1 [department_id] => 20 [department_name] => X ) [1] => stdClass Object ( [sla_id] => 38 [sla_name] => qweqweq [sla_days] => 1 [sla_hours] => 1 [sla_minutes] => 1  [department_id] => 21 [department_name] => Y ) )

提前致谢

1 个答案:

答案 0 :(得分:4)

尝试 -

    //Now getadded SLA with join of department
    $department_name = $name = $days = $id = array();
    foreach($this->model_admin->getaddedsla($lastentry->sla_id) as $returnsla) {
        $name[] = $returnsla->sla_name;
        $id[] = $returnsla->sla_id;
        $days[] = $returnsla->sla_days.'D '.$returnsla->sla_hours.'H '.$returnsla->sla_minutes.'M';
        $department_name[] = $returnsla->department_name;
    }
    $returnslajson['department_name'] = implode(',', $department_name);
    $returnslajson['sla_id'] = implode(',', $id);
    $returnslajson['sla_name'] = implode(',', $name);
    $returnslajson['sla_days'] = implode(',', $days);
    echo json_encode($returnslajson);