显示用户选择复选框选择和未定义的偏移错误

时间:2022-04-26 09:00:28

I would display the user choices made by ticking checkboxes. For some reason I don't understand, the array storing user choices displays as multidimensional array. The following is the code for declaring arrays:

我会通过勾选复选框显示用户选择。由于某些原因我不明白,存储用户选择的数组显示为多维数组。以下是声明数组的代码:

//declare the array
    $languages_array = array();

    //store user input into the array
    $languages_array[] = $_POST['languages'];

The following is where I display information from an array:

以下是我从数组中显示信息的位置:

print_r($languages_array);

This is the output I get:

这是我得到的输出:

Array ( [0] => Array ( [0] => xhosa_word [1] => zulu_word [2] => sepedi_word ) ) Isizulu :ilizwi

数组([0] =>数组([0] => xhosa_word [1] => zulu_word [2] => sepedi_word))Isizulu:ilizwi

Is this normal ? shouldn't this be a one dimensional array?

这是正常的吗?这不应该是一维数组吗?

I also get the following error:

我也收到以下错误:

Notice: Undefined offset: 3 in C:\xampp\htdocs\Dictionary\results_widget.php on line 53

注意:未定义的偏移量:第53行的C:\ xampp \ htdocs \ Dictionary \ results_widget.php中的3

This is the code for displaying the above output

这是显示上述输出的代码

                            print_r($languages_array);
                            for($i=0;$i<$array_count;$i++)
                            {

                                if($languages_array[0][$i] == 'zulu_word') //this is line 53
                                    {
                                        echo 'Isizulu :'.$row['zulu_word'];
                                    }

                            }       

2 个解决方案

#1


1  

Your problem lies here:

你的问题在于:

$languages_array = array();

//store user input into the array
$languages_array[] = $_POST['languages'];

You're creating a new array, and you are setting the first element of the array to $_POST['languages'].

您正在创建一个新数组,并且您正在将数组的第一个元素设置为$ _POST ['languages']。

What you probably want is this (you don't need the original array declaration):

你可能想要的是这个(你不需要原始的数组声明):

//store user input into the array
$languages_array = $_POST['languages'];

Update: As for your second problem... you have a one dimensional array, but you're treating it as two dimensional here:

更新:关于你的第二个问题......你有一个一维数组,但你在这里将它视为二维:

if($languages_array[0][$i] == 'zulu_word') //this is line 53

You probably want this:

你可能想要这个:

if($languages_array[$i] == 'zulu_word') //this is line 53

Also, what is the value of $array_count? You can just use count():

另外,$ array_count的值是多少?你可以使用count():

$array_count = count($languages_array);

#2


0  

Simply assign

简单地分配

$languages_array = $_POST['languages'];

$ languages_array = $ _POST ['languages'];

$_POST['languages'] is an array of fields with same name languages..

$ _POST ['languages']是一组具有相同名称语言的字段。

you can check if $_POST['languages'] is an array use var_dump($_POST['languages']);

你可以检查$ _POST ['languages']是否是一个数组使用var_dump($ _ POST ['languages']);

and use $languages_array[0]=='anything youwanna check'

并使用$ languages_array [0] =='任何youwanna check'

#1


1  

Your problem lies here:

你的问题在于:

$languages_array = array();

//store user input into the array
$languages_array[] = $_POST['languages'];

You're creating a new array, and you are setting the first element of the array to $_POST['languages'].

您正在创建一个新数组,并且您正在将数组的第一个元素设置为$ _POST ['languages']。

What you probably want is this (you don't need the original array declaration):

你可能想要的是这个(你不需要原始的数组声明):

//store user input into the array
$languages_array = $_POST['languages'];

Update: As for your second problem... you have a one dimensional array, but you're treating it as two dimensional here:

更新:关于你的第二个问题......你有一个一维数组,但你在这里将它视为二维:

if($languages_array[0][$i] == 'zulu_word') //this is line 53

You probably want this:

你可能想要这个:

if($languages_array[$i] == 'zulu_word') //this is line 53

Also, what is the value of $array_count? You can just use count():

另外,$ array_count的值是多少?你可以使用count():

$array_count = count($languages_array);

#2


0  

Simply assign

简单地分配

$languages_array = $_POST['languages'];

$ languages_array = $ _POST ['languages'];

$_POST['languages'] is an array of fields with same name languages..

$ _POST ['languages']是一组具有相同名称语言的字段。

you can check if $_POST['languages'] is an array use var_dump($_POST['languages']);

你可以检查$ _POST ['languages']是否是一个数组使用var_dump($ _ POST ['languages']);

and use $languages_array[0]=='anything youwanna check'

并使用$ languages_array [0] =='任何youwanna check'