动态关联创建(CodeIgniter)


Associative create dynamically (CodeIgniter)

我在代码点火器中有表单,我想使用辅助form_dropdown()。为了做到这一点,我必须准备这样的关联数组:

$options = array(
              'small'  => 'Samsung',
              'med'    => 'Apple',
              'large'   => 'HTC',
              'xlarge' => 'Nokia',
            );

但从这个角度来看,这些数据是从控制器传输的,当然,控制器是从模型中获取的。

        $this->db->select('id');
        $query  = $this->db->get('ci_table1');
    if ($query->num_rows() > 0 )
    {
        foreach ($query->result() as $row) 
        {
           $data[] = $row; 
        };
    };
    $id_data['id'] = $data;
    $this->load->view('update_record_view', $id_data);

因此,在视图的一侧,我有foreach循环:

 foreach ($id as $row) 
          {
             // this I want to construct associative array
          }

问题如下:如何在我的情况下动态创建关联数组?

我不明白你的代码。但也许这就是你要找的。

    $this->db->select('id');
    $id_data['id'] = $this->db->get('ci_table1')->result_array(); 
    $this->load->view('update_record_view', $id_data);        

和:

    $options = array();
    foreach ($id as $row) 
    {
        // this I want to construct associative array
        $options[ $row['id'] ] = ...;
    }