0

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?

4
  • E_NOTICE is a type of error_reporting. You can turn it off using ` error_reporting(E_ALL ^ E_NOTICE); Commented May 30, 2013 at 18:25
  • @JaredEitnier They are useful when they reveal logical problems, as they do in this case. If they are disabled, the OP would not know that his code was not working as expected. Commented May 30, 2013 at 18:26
  • To get a clear vission of what's going on you can print $set_controllers with var_dump($set_controllers). Commented May 30, 2013 at 18:34
  • $arr[] = $val is the equivalent of array_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. Commented May 30, 2013 at 18:43

1 Answer 1

7

Please try this:

$tmp = array();
$tmp['name'] = $input['name'];            //name
$tmp['answer'] = $input_value;            //answer
$tmp['required'] = $input['required'];    //is required field or not

$set_controllers[] = $tmp;

with [] you create a new index and i think you dont want that for each line.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. Yes this solved the problem. so changing this creates an array for every index. Thank so much for your quick answer. I will mark this answer correct in 10 minutes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.