php - 关联数组 - 动态创建 (CodeIgniter)

标签 php arrays codeigniter

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

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

但在这个 View 中,这些数据是从 Controller 传输的,当然,这些数据是从模型获取的。

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

所以,在 View 的一侧,我有 foreach-loop:

 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'] ] = ...;
    } 

关于php - 关联数组 - 动态创建 (CodeIgniter),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11879146/

相关文章:

mysql - SELECT 将检查超过 MAX_JOIN_SIZE 行

php in 子句不适用于准备好的语句

php - 如何使用模型处理 Laravel 中的数据库分区

java - 使用 http-post 和 AsyncTask 将数据插入 MySQL 不起作用

java - 如何创建一个 int 数组,其中包含给定范围内随机打乱的数字

codeigniter - 在 Application 目录外调用 CodeIgniter 的 Controller 方法

php - 文件版本控制以消除浏览器缓存问题

c# - 如何将此字符串拆分为数组?

c++ - 在另一个函数中使用返回的数组指针

php - 当值在 php 中较大时,隐藏的输入类型不会隐藏在表单中