Php 如何在CodeIgniter控制器中向阵列添加密钥?

Php 如何在CodeIgniter控制器中向阵列添加密钥?,php,codeigniter,Php,Codeigniter,我正在为用户创建简单的个人资料页面(用户主页,不让其他人看到访问)。我的控制器: <?php class Profile extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('Profile_model'); $th

我正在为用户创建简单的个人资料页面(用户主页,不让其他人看到访问)。我的控制器:

<?php
class Profile extends CI_Controller {

    public function __construct()
        {
                parent::__construct();
                $this->load->model('Profile_model');
                $this->load->database();
                if (!$this->tank_auth->is_logged_in()) 
                    redirect('auth/login');
        }

        public function index()
        {
                $user_id = $this->tank_auth->get_user_id();

                $data = $this->Profile_model->getUserAccountInfoById($user_id);
                $data[] = $this->Profile_model->getUserProfileInfoById($user_id);

                $this->load->view('profile/profile', $data);
        }
}

我的问题是关于向$data数组添加键/值。我试着用谷歌搜索,根据我发现的,我不得不添加[],但仍然不起作用(同样的错误)

使用
array\u merge
添加2个数组。您正在创建一个嵌套数组。
var\u dump($data)
将显示数组的内容

$data
是一个数组。当您创建
$data[]=…
时,您正在将另一个数组推送到
data
数组。因此,第二个数组现在是嵌套数组。
经度
现在不在数组的第一级。它在第二层(嵌套)


如果需要,也可以访问嵌套数组。首先打印内容,然后很容易决定如何访问嵌套数组使用
array\u merge
到marge 2数组。您正在创建嵌套数组。
var\u dump($data)
将显示数组的内容

$data
是一个数组。当您创建
$data[]=…
时,您正在将另一个数组推送到
data
数组。因此,第二个数组现在是嵌套数组。
经度
现在不在数组的第一级。它在第二层(嵌套)


如果需要,也可以访问嵌套数组。首先打印内容,然后很容易决定如何访问嵌套数组!关于同一主题,我还有其他问题。我做了一些实验,发现这也能起作用:在controller
$data['account\u data']=$this->Profile\u model->getUserAccountInfoById($user\u id);$data['profile\u data']=$this->profile\u model->getUserProfileInfoById($user\u id)
和view
你能解释一下区别吗,这样我就可以了解如何选择最佳选项了?@KārlisJanisels如果你创建
$data['account\u data']
,那么你就是在封装嵌套数组,这样你就可以通过
$data['profile\u data']
进行访问了。如果两个数组中的属性相同,则数组marge可能会发生冲突,以便更好地使用嵌套数组。否则,这两种方法都可以使用。如果没有必要,则无需嵌套数组。
var\u dump
非常有用。当您同时使用这两种方法时,您可以看到数组内容的差异。这很有效!关于同一主题,我还有其他问题。我做了一些实验,发现这也能起作用:在controller
$data['account\u data']=$this->Profile\u model->getUserAccountInfoById($user\u id);$data['profile\u data']=$this->profile\u model->getUserProfileInfoById($user\u id)
和view
你能解释一下区别吗,这样我就可以了解如何选择最佳选项了?@KārlisJanisels如果你创建
$data['account\u data']
,那么你就是在封装嵌套数组,这样你就可以通过
$data['profile\u data']
进行访问了。如果两个数组中的属性相同,则数组边距可能会发生冲突,以防更好地使用嵌套数组。否则,这两种方法都可以工作。如果不需要嵌套数组,则无需嵌套。
var\u dump
非常有用。当您同时使用这两种方法时,可以看到数组内容的差异。
<?php
class Profile_model extends CI_Model {

        public function __construct()
        {
                $this->load->database();

        }

        public function getUserAccountInfoById($user_id)
        {
                $query = $this->db->get_where('user_accounts', array('id' => $user_id));
                return $query->row_array();
        }
        public function getUserProfileInfoById($user_id)
        {
                $query = $this->db->get_where('user_profiles', array('user_id' => $user_id));
                return $query->row_array();
        }
}
<h2><?php echo $username; ?></h2>
<h2><?php echo $longitude; ?></h2>
<h2><?php echo $latitude; ?></h2>
Undefined variable: longitude
$data1 = $this->Profile_model->getUserAccountInfoById($user_id);
$data2 = $this->Profile_model->getUserProfileInfoById($user_id);
$data = array_merge($data1 , $data2 );
$this->load->view('profile/profile', $data);