如何将2D数组从jquery发送到php?

时间:2021-07-27 01:30:22

I have a 2D array which I want to send to a php page with $.ajax.

我有一个2D数组,我想用$.ajax发送到php页面。

This is the code which creates the array:

这是创建数组的代码:

for (var i = 0; i<rowlen; i++) {
                           if (breakcheck) {
                              break;
                           }
                           for (var j = 0; j<=columnlen; j++) {
                              thtext = columnheads.eq(j).text();
                              current_td = $(newrows[i]).find("td").eq(j);

                              if (current_td.find("input").length >0) {
                                 rowdata[i,thtext] = current_td.find("input").val().trim();
                                 if (rowdata[i,thtext] =='') {
                                    alert("You must complete all fields");
                                    breakcheck = true;
                                    break;
                                 }
                              } else {
                                 rowdata[i,thtext] ='nada';
                              }
                           }//inner loop
                        }//outer loop

The array is filled properly with the nested loops and the I use JSON.stringify to format it. However when the ajax call is made all that is sent is an empty object ([]). What's wrong?

数组被正确地填充了嵌套循环,而我使用了JSON。stringify格式。然而,当ajax调用完成时,所有发送的都是一个空对象([])。怎么了?

2 个解决方案

#1


2  

I might be wrong, but arr[i,j] is not the way to use multidimensional arrays in C-style languages. That would be arr[i][j].

我可能错了,但是arr[I,j]不是在c语言中使用多维数组的方法。这是加勒比海盗[我][j]。

IMHO what arr[i,j] will do is function as comma operator and use only j as an index.

arr[i,j]的作用是作为逗号运算符,只使用j作为索引。

#2


1  

OK I solved this by declaring r as an object (var r = {}) instead of declaring it as an array (var r = []). Thanks for the help.

我通过将r声明为对象(var r ={})而不是将其声明为数组(var r =[])来解决这个问题。谢谢你的帮助。

#1


2  

I might be wrong, but arr[i,j] is not the way to use multidimensional arrays in C-style languages. That would be arr[i][j].

我可能错了,但是arr[I,j]不是在c语言中使用多维数组的方法。这是加勒比海盗[我][j]。

IMHO what arr[i,j] will do is function as comma operator and use only j as an index.

arr[i,j]的作用是作为逗号运算符,只使用j作为索引。

#2


1  

OK I solved this by declaring r as an object (var r = {}) instead of declaring it as an array (var r = []). Thanks for the help.

我通过将r声明为对象(var r ={})而不是将其声明为数组(var r =[])来解决这个问题。谢谢你的帮助。