为关联数组codeigniter指定一个索引名称


Give a Index Name to associative array codeigniter

我使用关联数组来存储数据,但我在另一个关联数组中使用关联数组,我的代码如下

$field2 = array();
        for ($i = 0; $i < $numberofFilreds; $i++) {
            $fname = $this->input->post('mytext' . $i);
            array_push($field2, $fname = array(
                'type' => $this->input->post('DataTypes' . $i),
                'null' => TRUE,
            ));
        } 

当我运行我的代码时,我得到了像这样的数组

array(1) { [0]=> array(2) { ["type"]=> string(4) "text" ["null"]=> bool(true) } }

问题是我想要像这样的[0]=> array(2) ["Name"]=> array(2)我不知道怎么做请帮我

所以只需使用$field2["Name"] = array(...)。用您的唯一索引替换Name

for ($i = 0; $i < $numberofFilreds; $i++) {
    $fname = $this->input->post('mytext' . $i);
    $field2[$fname] = array(
        'type' => $this->input->post('DataTypes' . $i),
        'null' => TRUE,
    ));
}

如果您的$fname var是唯一的,请使用以下方法:

$fname = $this->input->post('mytext' . $i);
$$field2[$fname] = array(
        'type' => $this->input->post('DataTypes' . $i),
        'null' => TRUE,
    );