Error executing fields checklist

3

I have the following code:

$funcionario_materiais = array();
foreach($_POST AS $key=>$val){
    $tmp = explode("_",$key);
    if($tmp[0]=="materiais"){
        $funcionario_materiais[$tmp[1]]=$val;
        unset($_POST[$key]);
    }
}

$funcionario_materiais_novo = array();
for($i=0;$i<count($funcionario_materiais['material']);$i++){
    foreach($funcionario_materiais as $key=>$val){
        $funcionario_materiais_novo[$i][$key]= $funcionario_materiais[$key][$i];
        $funcionario_materiais_novo[$i]['idFuncionario'] = $ultimo_id;
        $funcionario_materiais_novo[$i]['data'] = date('Y-m-d', strtotime($funcionario_materiais_novo[$i]['data']));                
    }
}

But at run time, it gives an error:

  

A PHP Error was encountered

     

Severity: Notice
  Message: Undefined index: data
  Filename: controllers / funcionario.php
  Line Number: 246

This is line 246:

$funcionario_materiais_novo[$i]['data'] = 
    date('Y-m-d', strtotime($funcionario_materiais_novo[$i]['data']));

I think it's because inside the fields, some have been filled and some have not. For it is an array , and may come filled or not. But I do not know how to check whether the field is populated, within the foreach ...
If anyone can help, thank you very much!

    
asked by anonymous 14.06.2015 / 06:20

1 answer

0
// verifica se existe a posição no array
if (array_key_exists("data", $funcionario_materiais_novo[$i])) {
        // seu codigo
        $funcionario_materiais_novo[$i]['data'] = date('Y-m-d', strtotime($funcionario_materiais_novo[$i]['data']));
}

Put this inside your foreach. That should work. :)

    
15.06.2015 / 05:06