user2118095
user2118095

Reputation: 771

XML/Array to Key value pair in PHP

I have following output :

<table count="" time="0.010006904602051">
<item>
<id>607</id>
<name>MSPOT6071</name>
<description>Hip Hop / Raps</description>
<type>3</type>
<radio_folder_id/>
<albumart>
http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_175.jpg
</albumart>
<albumart_300>
http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_350.jpg
</albumart_300>
<albumart_500>
http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_500.jpg
</albumart_500>
</item>
<item>
<id>48542614</id>
<name>US Pop - TESTB</name>
<description>Blues</description>
<type>3</type>
<radio_folder_id/>
</item>
</table>

I want to read this output in the form of Key value pair and then want to store each value. for eg i want the value of "name"

Can anyone please help me with the code..I am not getting the clue of how to do.

Upvotes: 1

Views: 3202

Answers (2)

Jose Areas
Jose Areas

Reputation: 719

You can use simplexml extension functions to achieve your goals. With this extension you can load xml from file, from string or from a html node. Follow the links to get more information about each load function. I will explain here how to load using simplexml_load_file. This code:

<?php
echo "<pre>";   
$xml = simplexml_load_file("name.xml");
    print_r($xml);
    echo "</pre>";

?>

Will return this:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [count] => 
            [time] => 0.011766910552979
        )

    [item] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [id] => 607
                    [name] => MSPOT6071
                    [description] => Hip Hop / Raps
                    [type] => 3
                    [radio_folder_id] => SimpleXMLElement Object
                        (
                        )

                    [albumart] => http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_175.jpg
                    [albumart_300] => http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_350.jpg
                    [albumart_500] => http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_500.jpg
                )

            [1] => SimpleXMLElement Object
                (
                    [id] => 48542614
                    [name] => US Pop - TESTB
                    [description] => Blues 
                    [type] => 3
                    [radio_folder_id] => SimpleXMLElement Object
                        (
                        )

                )

        )

)

The information you want, can be accessed like this:

<?php
echo "<pre>";   
$xml = simplexml_load_file("name.xml");
    print_r($xml);
    echo "</pre>";
    echo "<pre>";
    foreach($xml->children() as $item){
        $arr = get_object_vars($item);
        foreach($arr as $key=>$value){
            echo "$key => $value" . PHP_EOL;            
        }
    }
    echo "</pre>";
?>

Note it will be accessed as object first. Then you can get all object vars to dynamically navigate through your object attributes.

Upvotes: 2

Michael Duncan
Michael Duncan

Reputation: 117

The

<?php

$xmlstr = '<?xml version="1.0" standalone="yes"?>
<table count="" time="0.010006904602051">
<item>
<id>607</id>
<name>MSPOT6071</name>
<description>Hip Hop / Raps</description>
<type>3</type>
<radio_folder_id/>
<albumart>
http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_175.jpg
</albumart>
<albumart_300>
http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_350.jpg
</albumart_300>
<albumart_500>
http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_500.jpg
</albumart_500>
</item>
<item>
<id>48542614</id>
<name>US Pop - TESTB</name>
<description>Blues</description>
<type>3</type>
<radio_folder_id/>
</item>
</table>';

$xml = new SimpleXMLElement($xmlstr);

foreach($xml->item as $item)
{
    echo $item->name."<br>";
}

echo $xml->item[0]->name;

echo '<pre>';
print_r($xml);
echo '</pre>';

?>

Assign your XML string to a variable $xmlstr and so you don't run into incomplete XML document errors make sure you include the following at the top of your XML document.

<?xml version="1.0" standalone="yes"?> 

Then use the built-in SimpleXML class by passing your XML string $xmlstr to SimpleXML:

$xml = new SimpleXMLElement($xmlstr);

Now you can access your XML file as a PHP object using the attributes and methods of the SimpleXML class. In this example we loop over the 'item'(s) in your XML document and print out the 'name' elements:

foreach($xml->item as $item)
{
    echo $item->name."<br>";
}

I have also include code to access the first item element:

echo $xml->item[0]->name;

As well as some debugging code view the XML document in a SimpleXML object:

echo '<pre>';
print_r($xml);
echo '</pre>';

You access the key or in this case the object property by its name. So in your foreach loop you might do:

if($item->name)
{
    echo $item->name;
}

or

if($item->description)
{
    echo $item->description;
}

May the force be with you.

Upvotes: 0

Related Questions