多维JavaScript数组到PHP数组

时间:2022-04-30 01:41:55

I have been trying to figure this out for awhile. I have a multidimensional array in JavaScript that is 12 columns wide and an unknown number of rows like so

我想弄明白这一点已经有一段时间了。我在JavaScript中有一个多维数组,有12列宽,行数未知

/*
[
    [userID (int), name, username, email, password, other 1, other 2, other 3, other 4, other 5, other 6, admin(int)],
    [userID (int), name, username, email, password, other 1, other 2, other 3, other 4, other 5, other 6, admin(int)],
    [userID (int), name, username, email, password, other 1, other 2, other 3, other 4, other 5, other 6, admin(int)],
    ...
]
*/

All the values are string except the (int) ones. This is for a real time user editing page for my site. I have the JavaScript array made now I need it so when the "Submit" button is pressed it turns the array formated like that into an PHP array and saves it as a variable. Can I get some help?

除了(int)值外,所有值都是字符串。这是我的站点的实时用户编辑页面。我已经制作了JavaScript数组,现在我需要它,所以当“提交”按钮被按下时,它将像这样格式化的数组转换为PHP数组并将其保存为变量。需要我帮忙吗?

2 个解决方案

#1


6  

Encode to JSON, post to PHP via Ajax or regular Form and decode on the serverside with json_decode

对JSON进行编码,通过Ajax或常规形式发布到PHP,并在服务器端使用json_decode进行解码

#2


0  

What you need to do is add the data to hidden fields in your form, if they are not already regular input fields.

您需要做的是将数据添加到表单中的隐藏字段中,如果它们不是常规的输入字段。

You probably want to use field naming like this:

您可能希望使用如下字段命名:

<input type="hidden" name="data[100][username]" value="myusername" />
<input type="hidden" name="data[100][email]" value="name@example.com" />

Where 100 is whatever each user id is.

100是每个用户id。

#1


6  

Encode to JSON, post to PHP via Ajax or regular Form and decode on the serverside with json_decode

对JSON进行编码,通过Ajax或常规形式发布到PHP,并在服务器端使用json_decode进行解码

#2


0  

What you need to do is add the data to hidden fields in your form, if they are not already regular input fields.

您需要做的是将数据添加到表单中的隐藏字段中,如果它们不是常规的输入字段。

You probably want to use field naming like this:

您可能希望使用如下字段命名:

<input type="hidden" name="data[100][username]" value="myusername" />
<input type="hidden" name="data[100][email]" value="name@example.com" />

Where 100 is whatever each user id is.

100是每个用户id。