【问题标题】:How to add the one more parameter in array using codeigniter如何使用codeigniter在数组中再添加一个参数
【发布时间】:2017-10-08 20:42:45
【问题描述】:

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

型号

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

    )

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    Use Like This run foreach 并把这个 $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); 
         }
    
        }
    

    现在在 print_r($result_new_student); 你会看到额外的 status 字段

    【讨论】:

    • 没有循环我们可以做还是不做?
    • 对于我的要求不需要使用任何循环,只要我想添加一个参数就足够了
    • 如果你想要每个子数组中的状态字段......那么我的方式你应该使用循环
    【解决方案2】:

    您应该将您的选择语句更改为:

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

    【讨论】:

      【解决方案3】:

      只需像这样添加:-

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

      请注意,您必须在 foreach($result_new_student as &$value) 中的 $value 之前添加 & 才能实际添加“状态”。

      【讨论】:

        【解决方案4】:

        1.select 查询中添加status,如下所示:-

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

        【讨论】:

        • @subikshanM 很高兴为您提供帮助:):)
        【解决方案5】:

        试试这个,

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

        【讨论】:

          猜你喜欢
          • 2015-08-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-03-20
          • 2022-01-25
          相关资源
          最近更新 更多