如何将值推送到全局声明的数组中

时间:2016-10-03 08:33:38

标签: php codeigniter

你好所有我在codeigniter工作,我的模型页面如下所示,我已经全局声明了数组是否可以在全局声明的$ referrals()数组中推送值

 <?php

    Class usermodel extends CI_Model {

               $referrals = array();

               function gettreferrals($uid,$referrals)

               {
                   $this->db->select('*');
                   $this->db->from('users');
                   $this->db->where('parent_id',$uid);
                   $this->db->order_by('user_id',ASC);
                   $query = $this->db->get();
                   $result = $query->result();
                   array_push($referrals,$result);  // pushing result in to array
               }

               function getdetails($usrid)
               {

                   $this->db->select('*');
                   $this->db->from('users');
                   $this->db->where('user_id',$usrid);
                   $queryes = $this->db->get();
                   $results = $queryes->result();
                   array_push($referrals,$results); //pushing in to array
               }
    }
    ?>

2 个答案:

答案 0 :(得分:1)

它不是一个全局变量。这是一个班级的财产。它可以这样做:

array_push($this->referrals,$result);
           ^^^^^

当您尝试访问该类的属性时,需要在其前面添加$this->

答案 1 :(得分:0)

你将不得不使用$ this来指向当前对象的$ refferels

     Class usermodel extends CI_Model {

           $referrals=array();

           function gettreferrals($uid,$referrals)

           {
                  $this->db->select('*');
                  $this->db->from('users');
                  $this->db->where('parent_id',$uid);
                  $this->db->order_by('user_id',ASC);
                  $query = $this->db->get();
                  $result = $query->result();

                  array_push($this->$referrals,$result);  // pushing result in to array


           }


           function getdetails($usrid)

           {

                          $this->db->select('*');
                          $this->db->from('users');
                          $this->db->where('user_id',$usrid);
                          $queryes = $this->db->get();
                          $results = $queryes->result();
                          array_push($this->$referrals,$results); //pushing in to array

           }

}