1

I have a xml with below info;

<item>
<site>Cambodia</site> 
<city>Phnom Penh</city> 
<code>21000</code >
</item>

I want to get all the info from this xml and input them into array $data, then echo them one by one like this; site = Cambodia; city = Phnom Penh; code = 21000; but i don't know how to do it. Please help me to solve this problem.

2
  • Did you try SimleXml? Commented Oct 9, 2013 at 4:15
  • Thanks all , I have done it now. ^^ Commented Oct 9, 2013 at 4:37

3 Answers 3

1

You should be able to just cast it to an array.

$data = new SimpleXMLElement($xml);
$array = (array) $data;
Sign up to request clarification or add additional context in comments.

Comments

0

you will get your desired output by the following code

 $xml = '
    <item>
    <site>Cambodia</site> 
    <city>Phnom Penh</city> 
    <code>21000</code >
    </item>';


    $data = new SimpleXmlElement($xml);
     $array = (array) $data;

     foreach($array as $key => $value){
     echo $key .'='. $value.'; ';  ///output:- site = Cambodia; city = Phnom Penh; code = 21000;
     }

Comments

0

This might do the trick!

Using xml_parse_into_struct — Parse XML data into an array structure

<?php
     $simple = "<item><site>Cambodia</site><city>Phnom Penh</city><code>21000</code>           </item>";

     $p = xml_parser_create();
     xml_parse_into_struct($p, $simple, $vals, $index);
     xml_parser_free($p);
     echo "Index array\n";
     print_r($index);
     echo "\nVals array\n";
     print_r($vals);
?>

OR SimpleXMLElement

$xml = new SimpleXMLElement($xmlString);

echo $xml->item->site;

$userArray = (array) $xml;
print_r($userArray)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.