Php 如何在变量中获取特定属性xml值
我有一个xmlPhp 如何在变量中获取特定属性xml值,php,xml,Php,Xml,我有一个xml <?xml version="1.0" encoding="UTF-8"?> <nm_response> <transaction> <transaction_id>111111111</transaction_id> <order_description></order_description> <first_name>Judi
<?xml version="1.0" encoding="UTF-8"?>
<nm_response>
<transaction>
<transaction_id>111111111</transaction_id>
<order_description></order_description>
<first_name>Judith</first_name>
<last_name>Grosz</last_name>
<address_1>addresse</address_1>
<address_2></address_2>
<company></company>
<city>Brooklyn</city>
<state>NY</state>
<postal_code></postal_code>
<country>US</country>
<email>test@test.com</email>
<phone>99999999999</phone>
<merchant_defined_field id="1">104647</merchant_defined_field>
<merchant_defined_field id="2">POS - 1347 - admin</merchant_defined_field>
<merchant_defined_field id="3">Judith Grosz</merchant_defined_field>
<merchant_defined_field id="4">8003452000</merchant_defined_field>
<merchant_defined_field id="5">40784.602953</merchant_defined_field>
<merchant_defined_field id="6">40784.602953</merchant_defined_field>
<original_transaction_id>1851413758</original_transaction_id>
<cc_bin>456331</cc_bin>
<action>
<amount>39.95</amount>
<action_type>sale</action_type>
<date>20140529081646</date>
<success>1</success>
<ip_address></ip_address>
</action>
</transaction>
<transaction>
<transaction_id>111111111</transaction_id>
<order_description></order_description>
<first_name>Judith</first_name>
<last_name>Grosz</last_name>
<address_1>addresse</address_1>
<address_2></address_2>
<company></company>
<city>Brooklyn</city>
<state>NY</state>
<postal_code></postal_code>
<country>US</country>
<email>test@test.com</email>
<phone>99999999999</phone>
<merchant_defined_field id="1">104647</merchant_defined_field>
<merchant_defined_field id="2">POS - 1347 - admin</merchant_defined_field>
<merchant_defined_field id="13">Judith Grosz</merchant_defined_field>
<merchant_defined_field id="14">8003452000</merchant_defined_field>
<merchant_defined_field id="25">40784.602953</merchant_defined_field>
<merchant_defined_field id="36">40784.602953</merchant_defined_field>
<original_transaction_id>1851413758</original_transaction_id>
<cc_bin>456331</cc_bin>
<action>
<amount>39.95</amount>
<action_type>sale</action_type>
<date>20140529081646</date>
<success>1</success>
<ip_address></ip_address>
</action>
</transaction>
</nm_response>
很好。
但我的问题是如何在不同的变量中定义所有的merchant__字段
$var1=value of merchant_defined_field id="2";
$var2=value of merchant_defined_field id="3"
$var3=value of merchant_defined_field id="13"
$var4=value of merchant_defined_field id="36"
$var1=$Array[“交易”][“商户定义字段”][0];
echo$var1.“
”;
$var2=$Array[“交易”][“商户定义字段”][1];
echo$var2.“
”;;
注意[“transaction”][“merchant_defined_field”]数组从索引0开始有一个偏移量尝试
${}
创建动态变量,并使用循环将值存储在
$i=1;
foreach($Array['transaction']['merchant_defined_field'] as $m) {
${"merchant_defined_field" . $i} = $m;
$i++;
}
echo $merchant_defined_field1;
echo $merchant_defined_field2;
echo $merchant_defined_field3;
echo $merchant_defined_field4;
要获取id属性,请尝试
$xml=simplexml_load_string($xml);
$i=1;
foreach($xml->transaction->merchant_defined_field as $m) {
${"merchant_defined_field_id" . $i} = $m->attributes()->id;
$i++;
}
echo $merchant_defined_field_id1;
echo $merchant_defined_field_id2;
echo $merchant_defined_field_id3;
echo $merchant_defined_field_id4;
试试这个:
$xml = simplexml_load_string($xml);
var_dump($xml->transaction->merchant_defined_field->attributes()->id);
现在检查get id attributes,但它是动态的,不是固定的。任何id都位于任何位置。请确保您的
xml
格式正确,因为它与OPxml
一起工作正常,请检查此处
$xml=simplexml_load_string($xml);
$i=1;
foreach($xml->transaction->merchant_defined_field as $m) {
${"merchant_defined_field_id" . $i} = $m->attributes()->id;
$i++;
}
echo $merchant_defined_field_id1;
echo $merchant_defined_field_id2;
echo $merchant_defined_field_id3;
echo $merchant_defined_field_id4;
$xml = simplexml_load_string($xml);
var_dump($xml->transaction->merchant_defined_field->attributes()->id);