javascript - Multidimensional Arrays via ajax to PHP - Stack Overflow

admin2025-04-04  0

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:

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 :

    main_array['peoplearray'] = peoplearray;

now to do the 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:

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

in firebug: (an empty string)

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

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

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!

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.

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?

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:

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 :

    main_array['peoplearray'] = peoplearray;

now to do the 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:

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

in firebug: (an empty string)

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

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

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!

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.

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?

Share Improve this question edited Mar 30, 2012 at 16:41 Zac asked Mar 30, 2012 at 15:08 ZacZac 1,6372 gold badges13 silver badges15 bronze badges 2
  • 1 can you post the entire JS function that makes the ajax request? if you use console.log(main_array) (before calling $.ajax) what does it show in firebug? – Mircea Soaica Commented Mar 30, 2012 at 15:20
  • I am getting the correct result with console.log(main_array) when doing it before the JSON.stringify. After the JSON.stringify I get: []. As far as the ajax that is all I am doing currently. In fact it is the only true copy and paste! Just to get to this point has taken me forever debugging arrays in Chrome is a no no I have just found out. – Zac Commented Mar 30, 2012 at 15:27
Add a ment  | 

2 Answers 2

Reset to default 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['peoplearray'] = peoplearray;

is equivalent to

main_array.peoplearray = peoplearray;

and you should declare main_array like this

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);

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: {}

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.

Then the ajax looked as follows:

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

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);
})
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1743750281a217435.html

最新回复(0)