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

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable