2

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

I use arr.length to check the size is 75. and I use firefox developer tool to check the data posted is normal. but I use sizeof($arr) 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.

javascript:

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

    $.post('getReport1/getReport1ExportXls',{
        table: $('#getReport1List').html(),
        data: data,
        myCol: myCol,
        myRow: myRow
    },function(o){
        openXls();
    });

`

PHP:

$data=$_POST['data'];

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.

4
  • Do you get expected results with a small array, e.g. 75x45? Commented May 6, 2016 at 3:48
  • the final array size is smaller. 13x75 and 1x72. but if i set the column like 32, the row I get may be greater like 43x32 and 1 x 16. So now I doubt the problem is due to the setting of php.ini . but I already tried. Commented May 6, 2016 at 3:50
  • I'm asking whether you're getting 3x2 array in PHP, if you pass 3x2 array from JavaScript. Commented May 6, 2016 at 3:52
  • if I only get 3x2 arry. it is normal. now I found the final array size i got will be close to 1000 if I want to get an array (size >1000) Commented May 6, 2016 at 3:54

1 Answer 1

1

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.

Update

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, my question before have misleading. the table should be 62 x 75. but finally I received is like (13 X 75 and 1 X 72 )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.