Error running Field Checklist

Asked

Viewed 31 times

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 when it’s time to run, it makes a mistake:

PHP Error was encountered

Severity: Notice
Message: Undefined index: data
Filename: controllers/funcio.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 is because within the fields, some have been filled and others have not.
For it is a array, and may or may not be filled in. But I don’t know how to verify whether or not the field is filled in foreach...
If anyone can help, I’d really appreciate it!

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. This should work. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.