使用jquery从php关联数组迭代循环


iterate loop from php associative array using jquery

我有这个PHP array storing code(数组的数据来自数据库查询),然后数组将被传递给JQuery。我的问题是,当我从php数组中检索数据时,会得到Undefined value。我在JQuery中使用$.Each循环。我的问题是,如何使用JQuery迭代phpassociative array的循环?

到目前为止,这是我的代码:

CONSOLE.LOG/网络映像

PHP数组存储

                                 **** loop here*****
$ListOfWorkOrderDates[$rowInnerJoin['WorkOrder']][$rowInnerJoin['DateField']] = 
                            array('DatePosted' => $rowInnerJoin['DatePosted']);
echo json_encode($ListOfWorkOrderDates);

Jquery循环

/// Here where I get confuse. How can I retrieve those data from php using jquery loop
$.ajax({
          url:'getWorkOrders.php',
          type:'POST',
          data:{id:_WOID},
          dataType:'json',
          success:function(output){
             console.log(output);
             $.each(output, function(index, object){
                   counter++;

                   ListOfPercentage.push(object.DateField);
                   ListOfDates.push(object.DatePosted);
             });    
          }
}); 
$ListOfWorkOrderDates[$rowInnerJoin['WorkOrder']][$rowInnerJoin['DateField']] = 
                            array('DatePosted' => $rowInnerJoin['DatePosted']);

您是否使用firebug或chrome-dev工具检查了您的确切json响应。

我相信对象。WorkOrder可能会返回undefined,因为您的json似乎没有WorkOrder密钥?

为了让js工作,您的json响应应该类似于

[
    {
        "WorkOrder": "w1",
        "DateField": "D1",
        "DatePosted": "DP1"
    },
    {
        "WorkOrder": "w2",
        "DateField": "D3",
        "DatePosted": "DP2"
    }
]

我用来生成上述响应的快速代码

$response=array();
    $response[0]=array("WorkOrder"=>"w1","DateField"=>"D1","DatePosted"=>"DP1");
    echo json_encode($response);