如何解析字符串到php数组?

时间:2014-05-20 06:31:32

标签: php arrays algorithm parsing xml-parsing

我有一个像这样的数组字符串:

 Array
(
    [TRANSACTION_ID] => SFBT-U1005141804-18DZW
    [TRANSACTION_REFERENCE] => KRZTV
    [TRANSACTION_TYPE] => sale
    [TRANSACTION_STATUS_CODE] => SA
    [MERCHANT] => safebytes
    [AFFILIATE] =>
    [TRANSACTION_DATE] => 2014-05-10 18:04:51
    [CUSTOMER_AMOUNT] => 39.90
    [CUSTOMER_CURRENCY] => USD
    [CUSTOMER_NAME] => Robin Jennings
    [CUSTOMER_EMAIL] => nesiesurveys@aol.com
    [CUSTOMER_COUNTRY] => US
    [CUSTOMER_ADDRESS] =>
    [CUSTOMER_CITY] =>
    [CUSTOMER_REGION] => NJ
    [CUSTOMER_ZIP_POSTAL] => 07067
    [CUSTOMER_LANGUAGE] => EN
    [CUSTOMER_PHONE_NUMBER] =>
    [CUSTOMER_IP] => 96.225.124.15
    [CUSTOMER_IP_ADDRESS] => 96.225.124.15
    [PAYMENT_METHOD] => PAYPAL
    [SKU] => totalsystemcare/1pk1yr
    [ORDER_STATUS] => SUCCESS
    [NOTES] => NONE
    [PAYOUT_CURRENCY] => USD
    [PAYOUT_AMOUNT] => 36.13
    [TAXES_AMOUNT] => 0.00
    [VOID] => N
    [QUERY] => sku%5B0%5D%3Dtotalsystemcare%2F1pk1yr%26ec%3D2404%26os%3DWindows+7%26osb%3D64bit%26pr%3DIntel%28R%29+Core%28TM%29+i3+CPU+++++++M+380++%40+2.53GHz%26mb%3DDell+Inc.+0THYJV%26vc%3DIntel%28R%29+HD+Graphics%26ram%3D3.80+GB%26hd%3D465.76+GB%26isp%3D%26UID%3DE294A80C-28B1-444B-935A-2D58F8A034D2%26det_country%3DUnited+States%26cou
    [EVENT_ID] => 1
    [PROCESSOR] => SafeCart
    [BILL_AMOUNT] => 39.9
    [ITEMS] => [{"SKU":"totalsystemcare\/1pk1yr","PRICE":"39.90000","QUANTITY":1,"PAYOUT":"36.12695","TAX":0,"RECURRING":true}]
)

此字符串写在txt文件中。我如何在php数组中解析它。我已经从文件中获取它但我无法将其解析为数组。

谁能帮助我吗?

1 个答案:

答案 0 :(得分:5)

您可以使用var_export函数将PHP变量保存到文件中。

例如:

$data = array('Foo', 'Bar');
file_put_contents('filename.txt', '<?php return ' . var_export($data, true) . ';');

// And get this content
$data = include 'filename.txt';