如何使用codeigniter在数组中添加一个参数

时间:2017-05-10 09:23:54

标签: php codeigniter

我在这个数组中有一个数组我想再添加一个参数,我不知道如何在这个数组中再添加一个参数。请任何人帮助我

模型

  public function android_memberList($mobile)
    {
        $this->db->select('new_student.student_id, new_student.firstName, new_student.user_type');
        $this->db->from('new_student');
        $this->db->where('fatherMobile', $mobile['mobile']);
        $query2 = $this->db->get();

        # result_array is used to convert the data into an array
        $result_new_student = $query2->result_array(); 
        print_r($result_new_student);

    }

print_r($result_new_student);我喜欢这个数组

 Array
(
    [0] => Array
        (
            [student_id] => 1
            [firstName] => janarthan
            [user_type] => Student
        )

    [1] => Array
        (
            [student_id] => 2
            [firstName] => Santanu
            [user_type] => Student
        )

)

在这个数组中,我们必须再添加一个参数,该参数应该包含在所有数组对象中

预期结果

Array
    (
        [0] => Array
            (
                [student_id] => 1
                [firstName] => janarthan
                [user_type] => Student
                [status] => "Verified"
            )

        [1] => Array
            (
                [student_id] => 2
                [firstName] => Santanu
                [user_type] => Student
                [status] => "Verified"
            )

    )

5 个答案:

答案 0 :(得分:4)

使用喜欢此操作foreach并将此 $ result_new_student [$ key] [' status'] ='已验证&#39 ;;

public function android_memberList($mobile)
    {
        $this->db->select('new_student.student_id, new_student.firstName, new_student.user_type');
        $this->db->from('new_student');
        $this->db->where('fatherMobile', $mobile['mobile']);
        $query2 = $this->db->get();

        # result_array is used to convert the data into an array
        $result_new_student = $query2->result_array(); 
        print_r($result_new_student);
        foreach($result_new_student as $key=>$result){
        $result_new_student[$key]['status'] = 'Verified';
        print_r($result_new_student); 
     }

    }

现在在 print_r($ result_new_student); ,您会看到额外的状态字段

答案 1 :(得分:3)

您应该将select语句更改为:

$this->db->select('new_student.student_id, new_student.firstName, new_student.user_type,"Verified" as status');

答案 2 :(得分:3)

只需添加如下: -

foreach($result_new_student as &$value)
{
    $value['status'] = "Verified";
}

请注意,您必须在foreach($ result_new_student & $ value)的$ value之前添加& 才能实际添加'status'。

答案 3 :(得分:2)

1。 也在status查询中添加select,如下所示: -

$this->db->select('new_student.student_id, new_student.firstName, new_student.user_type,new_student.status');

2。 正如您所说status列不存在的那样: -

foreach($result_new_student as $key=> $val){
  $result_new_student[$key]['status'] = 'Verified';
}

所以代码必须是这样的: -

public function android_memberList($mobile){
    $this->db->select('new_student.student_id, new_student.firstName, new_student.user_type');
    $this->db->from('new_student');
    $this->db->where('fatherMobile', $mobile['mobile']);
    $query2 = $this->db->get();

    //result_array is used to convert the data into an array
    $result_new_student = $query2->result_array(); 
    foreach($result_new_student as $key=> $val){
      $result_new_student[$key]['status'] = 'Verified';
    }
    print_r($result_new_student);

}

3。 一旦使用Virtual column concept并检查: -

$this->db->select('new_student.student_id, new_student.firstName, new_student.user_type,"Verified" as status');

答案 4 :(得分:2)

试试这个,

foreach( $result_new_student as $key=>$value ){ $result_new_student[$key]['status'] = "Verified"; } print_r($result_new_student);