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

时间:2014-10-20 03:18:53

标签: php jquery arrays

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

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

CONSOLE.LOG/NETWORK IMAGE

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);
             });    
          }
}); 

1 个答案:

答案 0 :(得分:0)

$ListOfWorkOrderDates[$rowInnerJoin['WorkOrder']][$rowInnerJoin['DateField']] = 
                            array('DatePosted' => $rowInnerJoin['DatePosted']);

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

我相信object.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);