1

私はこのXML構造を持っています:

<response status="success">
<campaign>
 <id>1</id>
 <nameCampaign>Raccolta Punti CCN</nameCampaign>
 <type>points</type>
 <balances>
     <balance>10.00 </balance>
  </balances>
 </campaign>
 <campaign>
 <id>6</id>
 <nameCampaign>Abbonamenti</nameCampaign>
 <type>subscription</type>
  <balances>
    <balance>6.00 Lampada solare</balance>
  </balances>
  <balances>
   <balance>4.00 doccia solare</balance>
  </balances>
  <balances>
     <balance>3.00 trifacciale</balance>
  </balances>
 </campaign>
</response>

そして、配列に変換する必要があります。

私が使用する場合

 foreach($result->campaign as $bilancio) {
    $balance[] = array  (   'nameCampaign'  =>  (string)$bilancio->nameCampaign,
                            'balances'      =>  (string)$bilancio->balances->balance    );      
} // foreach

この配列を取得します

    Array
  (
[0] => Array
    (
        [nameCampaign] => Raccolta Punti CCN
        [balances] => 10.00 
    )

[1] => Array
    (
        [nameCampaign] => Abbonamenti
        [balances] => 6.00 Lampada solare
    )
  )

ID 6 には 1 つだけでなく 3 つのサブノード (balances->balance) があります。完全な配列を取得するにはどうすればよいですか?

皆さんに感謝し、良い週末を!

4

1 に答える 1

1

読み取る代わりに(常にsimeplxmlでのみ最初の要素が得られます)、すべての要素を。で繰り返し処理しますforeach

方法は次のとおりです。

$balance = array();
foreach ($result->campaign as $campaign)
{
    $arr = array();
    foreach ($campaign->balances as $campaignBalance)
    {
        $arr[] = (string) $campaignBalance->balance;
    }

    $balance[] = array(
        'nameCampaign' => (string) $campaign->nameCampaign,
        'balances'     => $arr
    );
}

print_r($balance);

これはあなたにこの結果を与えるでしょう:

Array
(
    [0] => Array
        (
            [nameCampaign] => Raccolta Punti CCN
            [balances] => Array
                (
                    [0] => 10.00
                )
        )
    [1] => Array
        (
            [nameCampaign] => Abbonamenti
            [balances] => Array
                (
                    [0] => 6.00 Lampada solare
                    [1] => 4.00 doccia solare
                    [2] => 3.00 trifacciale
                )
        )
)
于 2012-12-15T14:12:37.120 に答える