如何使用PHP回显多维数组变量?

时间:2022-08-27 17:46:45

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?

为什么我会收到此错误?我该如何解决?

1 个解决方案

#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.

使用[]你创建一个新索引,我想你不希望每行。

#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.

使用[]你创建一个新索引,我想你不希望每行。