如何在JavaScript中获取php 2维数组

时间:2012-02-15 05:28:51

标签: php javascript json jquery

我正在使用jquery的ajax查询。我有3个模块或3个文件。第1个是表示数据的php文件,第2个是js文件,其中所有javascipt和ajax代码都写入,第3个是我的商务逻辑php文件。               我从我的第一个文件中调用了一个javascript函数,即show(id)。之后,js文件中的此函数调用ajax

function show(id){  

    // validation / client side.
    // function - lll to stringmatch - name proper.
    $.ajax({    
        type    : "POST",
        cache   : true,
        dataType: "json",
        url     : "a.php",
        data    : {
                    proid:id
                  },                
        success: function(data) {
         alert(data);
        //$("#match_item_display").html(data);



        }
    });



}

我的a.php文件返回json 2维数组... 之后我想在php或第一页中使用json 2 dimmentional数组。

请帮帮我们......

4 个答案:

答案 0 :(得分:1)

是的,数据将显示为[object Object]的{​​{1}}。

如果您想要分析返回的数据,则应使用alert()将其记录到Web控制台。

然后您可以自由使用回调中的数据。如果您从PHP返回了json编码对象,则甚至不必按摩数据。只需使用console.log(data);data.prop1 ...等

答案 1 :(得分:0)

循环json数组

function show(id){  

    // validation / client side.
    // function - lll to stringmatch - name proper.
    $.ajax({    
        type    : "POST",
        cache   : true,
        dataType: "json",
        url     : "a.php",
        data    : {
                    proid:id
                  },                
        success: function(data) {
          var str = '<div><ul>';
         for (i=0;i<data.length;i++){
                  str += '<li>'+data[i]['json_index']+'</li>';
            }
         str +='</ul></div>';




        }
    });



}

答案 2 :(得分:0)

您无法使用警报查看对象或阵列的内容。 如果你使用谷歌浏览器或firefox与firebug插件,使用console.log(data);而不是alert(data); 在加载页面以启用/显示开发人员工具栏之前按F12,您应该能够在萤火虫或镀铬的控制台中看到该对象。

祝你好运。

答案 3 :(得分:0)

var jsonData = [{"data":"oneone"},{"name":"onetwo"}];
for(var a in jsonData) {
    if(jsonData.hasOwnProperty(a)) {
        for(var b in jsonData[a]) {
            if(jsonData[a].hasOwnProperty(b)) {                                
                alert(jsonData[a][b]);
            }
        }
    }
}
​    ​