2017-07-11 58 views
-1

我有以下的关联数组($the_array):如何关联数组传递给JavaScript

Array 
(
    [NAME 1] => Array 
    (
     [0] => Array 
      (
       [transaction_id] => 6235944829 
       [transaction_date] => 30/06/2017 
       [transaction_description] => ALDI STORES  S 
       [transaction_amount] => 7 
      ) 

     [1] => Array 
      (
       [transaction_id] => 6229871969 
       [transaction_date] => 29/06/2017 
       [transaction_description] => CRUTHERLAND HOUSE 
       [transaction_amount] => 126 
      ) 

     [2] => Array 
      (
       [transaction_id] => 6229871971 
       [transaction_date] => 29/06/2017 
       [transaction_description] => MARKS&SPENCER PLC SACA 
       [transaction_amount] => 6.7 
      ) 

     [3] => Array 
      (
       [transaction_id] => 6229871975 
       [transaction_date] => 29/06/2017 
       [transaction_description] => HARRY RAMSDEN 
       [transaction_amount] => 10.43 
      ) 

    ) 

    [NAME 2] => Array 
    (
     [0] => Array 
      (
       [transaction_id] => 6203714807 
       [transaction_date] => 21/06/2017 
       [transaction_description] => MIDLAND HOTEL 
       [transaction_amount] => 122.1 
      ) 

     [1] => Array 
      (
       [transaction_id] => 6174035505 
       [transaction_date] => 14/06/2017 
       [transaction_description] => VIRGINTRAINSEC SERVCS 
       [transaction_amount] => 117 
      ) 

    ) 
) 

我怎样才能把它传递给jQuery的?这是我这么远,但它不工作:

在服务器端:

echo json_encode((object) [ 
         'uploaded' => true, 
         'message' => "test", 
         'match' => false, 
         'ac_array' => $the_array, 
         'ac_array_count' => $count_not_matched 
       ]); 

在客户端:

var arr = JSON.parse(result.ac_array); 
+2

你在服务器端有什么准确的?请注意,您需要脚本标记,并且需要将'json_encode()'的结果分配给javascript变量。而且你不需要投射到一个物体。 – jeroen

+0

那么'result.ac_array'是什么?您没有向我们显示任何创建该对象或设置属性的位置。 – CBroe

回答

0

在服务器端尝试

echo json_encode(array(
    'uploaded' => true, 
    'message' => "test", 
    'match' => false, 
    'ac_array' => $the_array, 
    'ac_array_count' => $count_not_matched 
)); 

and

在客户端尝试

var arr = jQuery.parseJSON(result); 
相关问题