次の xml を検討してください。
<?xml version="1.0" encoding="utf-8"?>
<PP>
<row merchant_id="xxxxxx" customer_code="xxxxxx" billing_name="xxxxxxx" account_status="Active" created_date="2013-08-01 08:26:31.710" last_modified_date="2013-08-01 08:26:52.170" last_trans_date="" billing_address1="" billing_address2="" billing_city="" billing_province_id="" billing_country_id="CA" billing_postal="" billing_email_address="mhussini@wilsons.ca" billing_phone="" velocity_group="" profile_group="" account_ref="987654" card_expiry="0813" cc_notification="" ref1="" ref2="" ref3="" ref4="" ref5="" />
<row merchant_id="xxxxx" customer_code="xxxxxxx" billing_name="xxx" account_status="Active" created_date="2013-08-04 14:52:38.277" last_modified_date="2013-08-05 21:07:01.233" last_trans_date="2013-08-05 03:00:01.043" billing_address1="" billing_address2="" billing_city="" billing_province_id="" billing_country_id="CA" billing_postal="" billing_email_address="" billing_phone="" velocity_group="" profile_group="" account_ref="" card_expiry="0813" cc_notification="" ref1="" ref2="" ref3="" ref4="" ref5="" />
</PP>
これにより、次のような simplexmlobject が得られます。
SimpleXMLElement Object ( [@attributes] => Array ( [merchant_id] => xxxxx [customer_code] => 987654 [billing_name] => John Doe [account_status] => Active [created_date] => 2013-08-01 08:26:31.710 [last_modified_date] => 2013-08-01 08:26:52.170 [last_trans_date] => [billing_address1] => [billing_address2] => [billing_city] => [billing_province_id] => [billing_country_id] => CA [billing_postal] => [billing_email_address] => mhussini@wilsons.ca [billing_phone] => [velocity_group] => [profile_group] => [account_ref] => 987654 [card_expiry] => 0813 [cc_notification] => [ref1] => [ref2] => [ref3] => [ref4] => [ref5] => ) )
次のようにさまざまな行にアクセスできることを知っています。
$xml = simplexml_load_string($resp)->row[0];
私が今できるようにしたいのは、インデックス付き配列として $xml 変数にアクセスするか、xml に渡される値を知らなくてもオブジェクトを反復処理できるようにすることです。
また、simplexmlobject で返される配列内のすべてのキーを取得できるようにしたいと考えています。
私はこのようなことができるようにしたいと思います:
for($i = 0;$i < count($xml);$i++){
$keys = array_keys($xml[$i]);
for($j = 0;$j < count($keys);$j++){
echo $keys[$j].' ';
echo $xml[$i][$j];
}
}