0

我从如下所示的 api 获取 XML 数据:

<?xml version="1.0" encoding="utf-8"?>
<PP>
<row merchant_id="" customer_code="" billing_name="" account_status="Active"  created_date="2013-07-31 13:22:32.687" last_modified_date="2013-07-31 13:23:41.827" last_trans_date="2013-07-31 13:35:59.257" billing_address1="rd." billing_address2="" billing_city="Lower" billing_province_id="" billing_country_id="CA" billing_postal="" billing_email_address="" billing_phone="" velocity_group="" profile_group="" account_ref="12345" card_expiry="0713" cc_notification="" ref1="" ref2="" ref3="" ref4="" ref5="" />
</PP>

我正在尝试访问 php 中的数据或将其转换为易于访问的数组。我尝试过这样的事情:

$xml = simplexml_load_string($resp);


$fields = array();
foreach ($xml->field as $f) {
    $f = (array) $f->attributes();
    $fields[] = $f['@attributes'];
}

但无法从中访问数据。

当我打印该数据时,它显示如下:

[account_status] => 活动 [created_date] => 2013-07-31 13:22:32.687 [last_modified_date] => 2013-07-31 13:23:41.827 [last_trans_date] => 2013-07-31 13:35: 59.257 [billing_address1] => rd。[billing_address2] => [billing_city] => Lowe [billing_province_id] => [billing_country_id] => CA [billing_postal] => [billing_email_address] => [billing_phone] => [velocity_group] => [profile_group] => [account_ref] => 12345 [card_expiry] => 0713 [cc_notification] => [ref1] => [ref2] => [ref3] => [ref4] => [ref5] => ) ) ) )

任何人都可以帮助我访问数据或帮助我将其放入数组中。

谢谢

4

1 回答 1

0

像这样访问它:

$xml = simplexml_load_string($x)->row; // assume XML in $x
echo $xml['billing_country_id'];

将输出

CA

这很简单,不是吗。

看到它工作:https ://eval.in/40171

于 2013-08-01T21:06:04.103 回答