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

时间:2021-02-06 00:25:18

I have two mysql table, sla and department, I am creating SLA and while creating sla, there is selection of multiple departments,

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

When user submit SLA form, respected departments in department table get sla id,

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

But after adding i need to pull that added SLA with join of departments which are updated,

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

Below is my code for controller,

下面是我的控制器代码,

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

I had added few comments at each line,

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

When SLA added i get only 1 department at output,

当SLA添加时,我只输出1个部门,

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

How can i get both department_name as X, Y

我怎样才能将department_name作为X,Y

Below is output of array,$this->model_admin->getaddedsla($lastentry->sla_id)

下面是数组的输出,$ 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 ) )

Thanks in advance

提前致谢

1 个解决方案

#1


Try with -

尝试 -

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

#1


Try with -

尝试 -

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