convert xml to array php – How to Convert XML File to Array in PHP?

convert xml to array php – you can use simplexml_load_string() amd The xml_parse_into_struct() function parses XML data into an array.

convert xml to array php

we will explain to you how to convert XML into associative array in PHP. Let’s see bellow example php xml to array example.

free_download_movie.xml

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<breakfast_menu>
<movie>
<name>tamilmv</name>
<amount>$5.95</amount>
<livecomments>TamilMV is a very malicious site to download motion pictures and other pilfered content on the web.</livecomments>
<movierank>650</movierank>
</movie>
<movie>
<name>mp3goo</name>
<amount>$7.95</amount>
<livecomments>Mp3goo - Download any song in mp3 format from our multi category Music databases.</livecomments>
<movierank>900</movierank>
</movie>
<movie>
<name>VegaMovies</name>
<amount>$8.95</amount>
<livecomments>Vegamovies is an online pirated website, which leaks movies illegally.</livecomments>
<movierank>900</movierank>
</movie>
<movie>
<name>Tamilrockers</name>
<amount>$4.50</amount>
<livecomments>Tamilrockers is a torrent website that is immensely popular among south Indian movie fans.</livecomments>
<movierank>600</movierank>
</movie>
<movie>
<name>Tamilblaster</name>
<amount>$6.95</amount>
<livecomments>TamilBlasters New Tamil Dubbed Movies Multi Audios Telugu Kannada Malayalam Hindi Download tamilblasters.</livecomments>
<movierank>950</movierank>
</movie>
</breakfast_menu>

PHP Code

index.php

<?php
  
    $xmlString = file_get_contents('free_download_movie.xml');
    $xmlObject = simplexml_load_string($xmlString);
              
    $json = json_encode($xmlObject);
    $phpArray = json_decode($json, true);
        
    print_r($phpArray);
?>

Output

Array
(
    [movie] => Array
        (
            [0] => Array
                (
                    [name] => tamilmv
                    [amount] => $5.95
                    [livecomments] => TamilMV is a very malicious site to download motion pictures and other pilfered content on the web.
                    [movierank] => 650
                )
            [1] => Array
                (
                    [name] => mp3goo
                    [amount] => $7.95
                    [livecomments] => Mp3goo - Download any song in mp3 format from our multi category Music databases.
                    [movierank] => 900
                )
            [2] => Array
                (
                    [name] => VegaMovies
                    [amount] => $8.95
                    [livecomments] => Vegamovies is an online pirated website, which leaks movies illegally.
                    [movierank] => 900
                )
            [3] => Array
                (
                    [name] => Tamilrockers
                    [amount] => $4.50
                    [livecomments] => Tamilrockers is a torrent website that is immensely popular among south Indian movie fans.
                    [movierank] => 600
                )
            [4] => Array
                (
                    [name] => Tamilblaster
                    [amount] => $6.95
                    [livecomments] => TamilBlasters New Tamil Dubbed Movies Multi Audios Telugu Kannada Malayalam Hindi Download tamilblasters.
                    [movierank] => 950
                )
        )
)

Don’t Miss : Convert Array To XML And XML To Array Using PHP

I hope you get an idea about convert xml to array php.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.