2017-07-11 59 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); 
相關問題