0

I want to convert this xml output:

<?xml version="1.0" encoding="UTF-8"?>
<levela xmlns="https://restapi" version="1.0.0">
 <levelb>
   <levelc>
     <var1>01</var1>
     <var2>String1</var2>
   </levelc>
   <levelc>
     <var1>02</var1>
     <var2>String2</var2>
   </levelc>
   <levelc>
     <var1>08</var1>
     <var2>String3</var2>
   </levelc>
 </levelb>
</levela>

to this php array:

array(
    '01' => 'String1',
    '02' => 'String2',
    '08' => 'String3'
  )

I tried in many ways, but it's more difficult than I thought (for me). I hope someone can help me. Many thanks in advance!

brasileric
  • 1,017
  • 1
  • 8
  • 18
  • Possible duplicate of [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – Andy Hoffner Mar 23 '16 at 21:57
  • I'm voting to close this question as off-topic because it is a "write my code" type question – michi Mar 24 '16 at 00:44

2 Answers2

1

It's an easy task with SimpleXML:

Load the XML into a SimpleXML object:

$xml = simplexml_load_string( $string );

Perform a foreach() loop through all <issuer> nodes and add <issuername> child node text value to your array using <issuerid> as key:

$result = array();
foreach( $xml->directory->issuer as $node )
{
    $result[ $node->issuerid->__toString() ] = $node->issuername->__toString();
}

print_r( $result );

The output is:

Array
(
    [01] => ABN Amro Bank
    [02] => ASN Bank
    [08] => Friesland Bank
)

SimpleXML return an object, so you need to cast as string node values with ->toString() method to add it as string.


fusion3k
  • 11,568
  • 4
  • 25
  • 47
  • Apparently the last entry in the array should be `[08] => 'Friesland Bank'` , you may have to change a bit your code. – Loufylouf Mar 23 '16 at 21:30
  • @Loufylouf Yes, you are right. I did not see the use of `issuerid` as key. Thank you for the catch! Answer edited. – fusion3k Mar 23 '16 at 21:34
0

You can use the follwong little trick to first get an array.

$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$array = json_decode($json,TRUE);

The result of this will be an array representing exactly the XML structure, like that :

Array
(
    [@attributes] => Array
        (
            [version] => 1.0.0
        )

    [directory] => Array
        (
            [issuer] => Array
                (
                    [0] => Array
                        (
                            [issuerid] => 01
                            [issuername] => ABN Amro Bank
                        )

                    [1] => Array
                        (
                            [issuerid] => 02
                            [issuername] => ASN Bank
                        )

                    [2] => Array
                        (
                            [issuerid] => 08
                            [issuername] => Friesland Bank
                        )

                )

        )

)

With just a little bit of work, you will be able to build the array you want.

Loufylouf
  • 697
  • 5
  • 13