dongxun1244 2016-02-09 20:05
浏览 127
已采纳

通过jquery发送的关联php数组返回[Object object]

I have to pass a php multidimensional array via ajax/jQuery to another php file. I have encoded the array using I expected theat every item in the array would have been an array itself. Instead it returns [Object object]. How can I access the data using php?

here's my code:

in the first php file:

<script type="text/javascript">
var arr_data = <?php echo json_encode($itemsData); ?>;
$("#confirmButton").click(function(){
    $.post("send_test.php", {"my_array_data[]":my_array_data}, function( data ) {
        alert(data);
    });
});
</script>

the other php file:

<?php
$my_array_data = $_POST['my_array_data'];
?>

if I try to retrieve the first row ($my_array_data[0]) I get [Object object] I just want to access to $my_array_data[0]['category'] etc.

  • 写回答

3条回答 默认 最新

  • douzi8548 2016-02-09 20:26
    关注

    A couple of errors here:

    • the data you are passing to ajax has an incorrect key using brackets[]
    • you are not passing the correct object through ajax since my_array_data is never defined

    redo your code like this:

    PHP

    $itemsData = array(
        array(
            "test" => 30,
            "test2" => 10,
        ),
        array(
            "test" => 90,
            "test2" => 50,
        )
    );
    

    JS

    var arr_data = <?php echo json_encode($itemsData); ?>;
    $("#confirmButton").click(function () {
        $.post("send_test.php", {my_array_data: arr_data}, function (data) {
            console.log(data);
        });
    });
    

    Then in send_test.php

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

    Result:

    Array
    (
        [0] => stdClass Object
            (
                [test] => 30
                [test2] => 10
            )
    
        [1] => stdClass Object
            (
                [test] => 90
                [test2] => 50
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料