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!

2
  • Possible duplicate of How do you parse and process HTML/XML in PHP? Commented Mar 23, 2016 at 21:57
  • I'm voting to close this question as off-topic because it is a "write my code" type question Commented Mar 24, 2016 at 0:44

2 Answers 2

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.


Sign up to request clarification or add additional context in comments.

2 Comments

Apparently the last entry in the array should be [08] => 'Friesland Bank' , you may have to change a bit your code.
@Loufylouf Yes, you are right. I did not see the use of issuerid as key. Thank you for the catch! Answer edited.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.