dongxun1244
2016-02-09 20:05
浏览 111

通过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.

图片转代码服务由CSDN问答提供 功能建议

我必须通过ajax / jQuery将php多维数组传递给另一个php文件。 我已使用 我希望数组中的每个项都是一个数组本身。 相反,它返回[Object object]。 如何使用php访问数据?

这是我的代码:

在第一个php文件中:

 &lt; script type =“text / javascript”&gt; 
var arr_data =&lt;?php echo json_encode($ itemsData);  ?&gt ;; 
 $(“#confirmButton”)。click(function(){
 $ .post(“send_test.php”,{“my_array_data []”:my_array_data},function(data){
 alert  (数据); 
}); 
}); 
&lt; / script&gt; 
   
 
 

其他php文件:

 &lt;?php 
 $ my_array_data = $ _POST ['my_array_data']; 
?&gt; 
   
 
 

如果我尝试检索 第一行( $ my_array_data [0] )我得 [Object object] 我只想访问 $ my_array_data [0] ['category'] 等。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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
            )
    
    )
    
    点赞 评论
  • dstd2129 2016-02-09 20:13

    You're putting json-encoded data into the arr_data javascript variable - however you don't appear to be sending that to the other php file.

    In the other php file, try using json_decode on the data you're receiving in the POST request:

    $my_array_data = json_decode($_POST['my_array_data']);
    
    点赞 评论
  • duanfan1965 2016-02-09 20:15

    Yes, as Aric says, name array consistently like this:

    var my_arr_data = <?php echo json_encode($itemsData); ?>;
    
    点赞 评论

相关推荐 更多相似问题