如何将关联数组传递给javascript

时间:2017-07-11 09:19:38

标签: php jquery

我有以下关联数组($the_array):

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

1 个答案:

答案 0 :(得分:0)

在服务器端尝试

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

在客户端尝试

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