dongzeao5047 2013-02-13 13:22
浏览 35
已采纳

具有多维数组的Javascript

I am having a problem with my multidimensional array. I am trying to insert an element via AJAX.

I grab the data from inside a table and send it to a PHP file which is meant to print the results but it prints incorrect data.

My HTML table:

<table id="mytable">
   <tr>
       <td>first</td>
       <td>second</td>
       <td>third</td>
   </tr>
   <tr>
       <td>fourth</td>
       <td>fifth</td>
       <td>sixth</td>
   </tr>
</table>

Here is my Javascript:

for(i=0; i<=length; i++){  
    for(j=0;j<width;j++){

    //inside of html table grab cell 1x1
            data = document.getElementById("mytable").rows[i].cells[j].innerHTML;

            //define the Array 
            export_table[i]= new Array;
            export_table[i][j] = data;
    }
}

The PHP in my processdata.php:

print_r($_POST)

The problem I see is PHP prints:

Array
        (
            [0] => ,,first
            [1] => ,,,second
        )

But I really need it to print:

Array
        (
            [0] => Array
                              (
                                 [0] => "first"
                                 [1] => "second"
                                 [2] => "third"
                              )
            [1] =>  Array
                              (
                                 [0] => "fourth"
                                 [1] => "fifth"
                                 [2] => "sixth"
                              )
        )
  • 写回答

2条回答 默认 最新

  • duanbu1998 2013-02-13 13:26
    关注

    From what I can see from your Javascript it looks like you are sending your Javascript array variable to the PHP.
    If this is true then the array value will not send properly because you can not pass advanced data types like Arrays and Objects into PHP $_POST and $_GET queries.
    You may need to send it as a serialized string and then explode() the data inside PHP to move it back into a PHP array variable.

    Alternatively you could try this:

    for(i=0; i<=length; i++){      
           //The first MultiDimensional Array
            export_table[i]= new Array;
            for(j=0;j<width;j++){
            //grab tables cell 1x1
                    data = document.getElementById("mytable").rows[i].cells[j].innerHTML;
                   //insert 'data' into 2nd dimentional array
                    export_table[i][j] = data;
            }
        }
    

    It should return values in format below:

    Array
    (
        [tab] => Array
            (
                [0] => first,second,third
                [1] => fourth,fifth,sixth
            )
    
    )
    

    Now PHP can use explode to move each array item into its own array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作