通过ajax到PHP的多维数组

时间:2022-05-27 02:09:27

Ok seriously struggling here. I am having some problems trying to send a multdimensional array to PHP via ajax. Here's what I have been trying:

好认真在这里挣扎。我试图通过ajax向PHP发送多维数组时遇到一些问题。这是我一直在尝试的:

To simplify rather than copy paste a wall of code:

为了简化而不是复制粘贴代码墙:

    peoplearray[0]  =  [name] => 'john'
                       [age]  => '28'
                       [sex]  => 'Male'
    peoplearray[1]  =  [name] => 'julie'
                       [age]  => '20'
                       [sex]  => 'Female'

    main_array['item'] = 'x';
    main_array['something'] = 'x';
    main_array['another'] = 'x';

I want to get this to php via post. I figured I may aswell just join them together as I am multidimensional anyway thus :

我希望通过帖子将其发送到php。我想我也可以将它们加在一起,因为我是多维的,因此:

    main_array['peoplearray'] = peoplearray;

now to do the ajax:

现在要做ajax:

// var data = JSON.stringify(main_array);

var send = $.ajax({
        type: "POST",
        cache: false,
        url: "theurl",
        data: {data:main_array} //I do change this `main_array` when using the above stringify!
});


send.done(function(msg) {
console.log(msg);
})

in PHP I am just doing the following right now:

在PHP中,我现在正在执行以下操作:

$data= $_POST['data'];
print_r($data);

in firebug: (an empty string)

在萤火虫:(一个空字符串)

when I have the var data = JSON.stringify(main_array); uncommented I get the following: [][

当我有var data = JSON.stringify(main_array);取消注释我得到以下内容:[] [

if i add $data = json_decode($_POST['data']); to the php I get:

如果我添加$ data = json_decode($ _ POST ['data']);我得到的PHP:

Array ( )

Basically the main_array I realise does not need to be an array and so I can get that stuff across no problem but what I need to do is get the peoplearray over so that I can do some foreach etc... with it in php. Any help would be much appreciated I am sure I am just being stupid!

基本上我认识的main_array不需要是一个数组,所以我可以得到那些东西没有问题,但我需要做的是让人们发布,以便我可以做一些foreach等...用它在PHP中。任何帮助将不胜感激我相信我只是愚蠢!

EDIT: The reasoning behind this is that peoplearray could have 0 or 100 entries so I just need to get it to php so I can foreach it to do the DB inputs. If there is a better approach I would be very grateful to hear it as I am still pretty new to this.

编辑:这背后的原因是peoplearray可能有0或100个条目,所以我只需要将它转换为php,这样我就可以预先做它来进行数据库输入。如果有一个更好的方法,我会非常感激听到它,因为我仍然是新手。

EDIT: Thanks to Nicola's answer everything is passing fine except the important part which is mainarry.peoplearray - it is not appearing in the the return console.log and I cant access it in PHP. Any solutions on this or do I have to put the foreach intelligence in the javascript and just send everything individually?

编辑:感谢Nicola的回答一切正常,除了mainarry.peoplearray的重要部分 - 它没有出现在return console.log中,我无法在PHP中访问它。任何解决方案或我是否必须将foreach智能放在javascript中并单独发送所有内容?

2 个解决方案

#1


7  

First of all main_array is not an array but an object because in javascript there are no associative arrays and for this reason

首先,main_array不是一个数组而是一个对象,因为在javascript中没有关联数组,因此

main_array['peoplearray'] = peoplearray;

is equivalent to

相当于

main_array.peoplearray = peoplearray;

and you should declare main_array like this

你应该像这样声明main_array

var main_array = {};

then try to change your function like this:

然后尝试改变你的功能:

var send = $.ajax({
        type: "POST",
        dataType: "json",
        cache: false,
        url: "theurl",
        data: {data:main_array} 
});

and server side

和服务器端

header('Content-type: application/json');
$data= $_POST['data'];
echo json_encode($data);

#2


1  

I got it to work by keeping the peoplearray seperate.

我通过保持人员分离来实现它。

So I did as Nicola said and created mainarray as an object ie. declaring with curlies: {}

所以我按照尼古拉的说法做了,并创建了主阵列作为对象,即。用curlies声明:{}

The peoplearray I left as an array ie declaring with [], however then name,age&sex fields I created as an object ie. {} and then .push() them into the the peoplearray.

我作为一个数组离开的人员阵列,即用[]声明,然而我创建的名称,年龄和性别字段作为对象即。 {}然后.push()他们进入了人员阵列。

Then the ajax looked as follows:

然后ajax看起来如下:

var send = $.ajax({
        type: "POST",
        dataType: "json",
        cache: false,
        url: "theurl",
        data: {data:main_array, people:peoplearray} 
});

then with the PHP everything is available in the $_POST, and if you

然后使用PHP,$ _POST中有一切可用,如果你

echo json_encode($people); //or whatever var name it is stored as in the php 

the objects ie name,age,sex properties are shown in the

对象即名称,年龄,性别属性显示在

send.done(function(msg) {
console.log(msg);
})

#1


7  

First of all main_array is not an array but an object because in javascript there are no associative arrays and for this reason

首先,main_array不是一个数组而是一个对象,因为在javascript中没有关联数组,因此

main_array['peoplearray'] = peoplearray;

is equivalent to

相当于

main_array.peoplearray = peoplearray;

and you should declare main_array like this

你应该像这样声明main_array

var main_array = {};

then try to change your function like this:

然后尝试改变你的功能:

var send = $.ajax({
        type: "POST",
        dataType: "json",
        cache: false,
        url: "theurl",
        data: {data:main_array} 
});

and server side

和服务器端

header('Content-type: application/json');
$data= $_POST['data'];
echo json_encode($data);

#2


1  

I got it to work by keeping the peoplearray seperate.

我通过保持人员分离来实现它。

So I did as Nicola said and created mainarray as an object ie. declaring with curlies: {}

所以我按照尼古拉的说法做了,并创建了主阵列作为对象,即。用curlies声明:{}

The peoplearray I left as an array ie declaring with [], however then name,age&sex fields I created as an object ie. {} and then .push() them into the the peoplearray.

我作为一个数组离开的人员阵列,即用[]声明,然而我创建的名称,年龄和性别字段作为对象即。 {}然后.push()他们进入了人员阵列。

Then the ajax looked as follows:

然后ajax看起来如下:

var send = $.ajax({
        type: "POST",
        dataType: "json",
        cache: false,
        url: "theurl",
        data: {data:main_array, people:peoplearray} 
});

then with the PHP everything is available in the $_POST, and if you

然后使用PHP,$ _POST中有一切可用,如果你

echo json_encode($people); //or whatever var name it is stored as in the php 

the objects ie name,age,sex properties are shown in the

对象即名称,年龄,性别属性显示在

send.done(function(msg) {
console.log(msg);
})