How to add another parameter to an array using codeigniter

I have one array in this array. I want to add another parameter, I cannot add another parameter to this array. Please help me

model

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

I have an array like this

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

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

)

      

In this array, we must add one more parameter, this parameter must appear in the whole array object

Expected results

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"
            )

    )

      

+3


source to share


5 answers


1. Add status

also to the select

query as below: -

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

      

2. As you said, the column status

does not exist: -

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

      



So the code should be like this: -

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. After using the Virtual Column Concept and check: -

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

      

+2


source


Use foreach as this run and put this $ 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(); 
        print_r($result_new_student);
        foreach($result_new_student as $key=>$result){
        $result_new_student[$key]['status'] = 'Verified';
        print_r($result_new_student); 
     }

    }

      



now in print_r ($ result_new_student); you will see the status field

+4


source


You have to change the select statement:

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

+3


source


Just add like this: -

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

      

Note that you need to add & before $ value in foreach ($ result_new_student as & $ value) to add status.

+3


source


Try it,

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

      

+2


source







All Articles