This question may be very simple and easy but unfortunately I can't answer it correctly.
I have a data set that I obtained from the database then based on a condition I create a new "data set" or a multidimensional array finally I am ready to display what's in the final array. The reason why I am using a second array if because I need to use it for other purposes.
This is my current code
//$controllers is a data set returned from a mysql query
$set_controllers = array();
foreach($controllers AS $input){
$input_value = '';
if(isset($_POST[$input['name']]) ){
$input_value = trim($_POST[$input['name']]);
}
$set_controllers[]['name'] = $input['name']; //name
$set_controllers[]['answer'] = $input_value; //answer
$set_controllers[]['required'] = $input['required']; //is required field or not
}
foreach($set_controllers AS $field){
echo $field['name'] . '<br />';
echo $field['required'] . '<br />';
echo $field['answer'] . '<br /><br />';
}
The problem that I am having is:
Notice: Undefined index: required
Undefined index: name
Undefined index: answer
Why do I get this error? How can I solve it?
E_NOTICEis a type of error_reporting. You can turn it off using `error_reporting(E_ALL ^ E_NOTICE);var_dump($set_controllers).$arr[] = $valis the equivalent ofarray_push($arr, $val). So you're pushing three separate values in three separate push operations, creating three new array entries. Each of those entries will have only one of those three key/value pairs you trying to save, hence your errors.