문제

XML

<?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>
.

first_name, last_name 등과 같은 값을 가져옵니다. 아래의 코드를 사용하여 배열로 변환

$xml   = simplexml_load_string($xml);
$Class = json_encode($xml);
$Array = json_decode($Class,TRUE);
$fetch_trans =  $Array['transaction']; 
.

그것은 잘 작동합니다. 그러나 내 문제는 다른 merchant_defined_field를 diffrent-diffrent varibales

에서 얻는 방법입니다.
$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" 
.

올바른 솔루션이 없습니다

다른 팁

시도 :

$xml = simplexml_load_string($xml);
var_dump($xml->transaction->merchant_defined_field->attributes()->id);
.

데모 링크

${}를 사용하여 동적 변수를 만들고 루프를 사용하여

값을 저장합니다.
$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;
.

$var1=$Array["transaction"]["merchant_defined_field"][0];

echo $var1 ."<br>";

$var2=$Array["transaction"]["merchant_defined_field"][1];

echo $var2."<br>";;
.

[ "transaction"] [ "merchant_defined_field"]의 오프셋이 인덱스 0에서 시작합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top