从数组中获取数据无效

时间:2013-01-22 11:25:27

标签: php arrays codeigniter multidimensional-array

根据我的观点,我有以下表格:

<table align="center" border="1">
<tr>
   <td>Identificação</td>
   <td>Conc, %</td>
   <td>Classificação 67/548/CEE</td>
   <td>Classificação 1272/2008 (CLP)</td>
</tr>
<tr>
   <td>
       <textarea rows="4" cols="30" name="componentes[0][identificacao]"></textarea>
   </td>
   <td>
       <textarea rows="4" cols="30" name="componentes[0][conc]"></textarea>
   </td>
   <td>
       <textarea rows="4" cols="30" name="componentes[0][classificacao_cee]"></textarea>
   </td>
   <td>
       <textarea rows="4" cols="30" name="componentes[0][classificacao_clp]"></textarea>
   </td>
</tr>
</table>

<div id="outro_curso"></div>
<p class="submit">
    <button id="novo_curso">Add Curso</button>
</p>

如您所见,我正在使用多维数组。我这样做,因为当我点击“添加Curso”按钮时,我调用一个jquery函数,生成另一个像前一个表。文本区域的名称将为componentes[1][identificacao]componentes[1][conc]等...

注意:我可以使用该按钮生成我想要的次数。

现在,我的问题是如何在我的CodeIgniter模型中处理这些数据。我尝试将数据保存到$componentes数组(以后插入数据库)但我想我的代码有问题:

$componentes;
    foreach ($this->input->post('componentes') as $key => $value){
        $componentes[$key]['identificacao']=$value[$key]['identificacao'];
        $componentes[$key]['conc']=$value[$key]['conc'];
        $componentes[$key]['classificacao_cee']=$value[$key]['classificacao_cee'];
        $componentes[$key]['classificacao_clp']=$value[$key]['classificacao_clp'];
    }

有人能给我一点帮助吗?

修改

我忘了提,我收到了错误:

  

为foreach()提供的参数无效。

所以我不知道我的foreach ($this->input->post('componentes') as $key => $value){是否正确,或者内部的行是否有问题。

1 个答案:

答案 0 :(得分:1)

$componentes_post = $this->input->post('componentes');
if(is_array($componentes_post)&&!empty($componentes_post))
{
    foreach ($componentes_post as $key => $value)
    {
        $temp_componentes['identificacao']=$value['identificacao'];
        $temp_componentes['conc']=$value['conc'];
        $temp_componentes['classificacao_cee']=$value['classificacao_cee'];
        $temp_componentes['classificacao_clp']=$value['classificacao_clp'];
        $componentes[] = $temp_componentes;
    }
}

现在看,

print_r($componentes);

你应该得到你需要的东西。 接下来,您收到错误为foreach()提供的参数无效,因为您在foreach上迭代的数组为空,请确保使用print_r($this->input->post('componentes'))在该元素上显示值