2013-08-06 37 views
0

考慮下面的XML:訪問simplexmlobject動態

<?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="[email protected]" 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] => [email protected] [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]; 
    } 
} 

回答

0

類似的東西,我覺得(有PP $您的SimpleXMLElement):

foreach ($PP->PP->row as $data) { 
    foreach ($data as $attribute) { 
    print_r($data); 
}