Kenneth Chan Kenneth Chan - 1 year ago 40
Javascript Question

pass two dimensional array from javascript to php but the size were reduced

I want to pass a 2D array from javascript to PHP. but the result I get only part of the array.

I use

to check the size is 75.
and I use firefox developer tool to check the data posted is normal.
but I use
in PHP to check the size of 43.

I use apache as the server.
the content of array is string but not more than 10 character.

the following is the code I used.


myCol= 0 + $('#tabResult th').length; //75
myRow= 0 + $('#tabResult tr').length; //62

var data = new Array(myRow);
for(i = 0; i < myRow; i++){
data[i] = new Array(75);

table: $('#getReport1List').html(),
data: data,
myCol: myCol,
myRow: myRow




Update 1:
In fact, the final array size(row x column) is closely to 1000. I already try to change php.ini
max_input_vars = 30000

and run
httpd -k restart

but no effect.

Answer Source

The data variable is an array of myRow items:

var data = new Array(myRow);

And each item is an array of 75 elements:

data[i] = new Array(75);

So you're generating an array of size myRow x 75:

data = [
  item_1: [ ... 75 items ... ]
  item_myRow: [ ... 75 items ... ]

In PHP you should get the same:

count($_POST['data']); // "myRow"
count($_POST['data'][0]); // 75

According to your question, myRow equals to 43.


I recommend to pack the array in JSON:

data: JSON.stringify(data)

then decode it in PHP:

$data = json_decode($_POST['data'], true);

Then you'll be limited only to the post_max_size and to the memory_limit, of course.