0

I have following string

<DReport>
    <message 
        id="1023012301" 
        sdate="2005/7/19 22:0:0" 
        ddate="2005/7/19 22:0:0"
        status="N" />
</DReport>

How to parse this string as a PHP array

$report = array(
    "id"=>"1023012301",
    "sdate"=>"2005/7/19 22:0:0",
    "ddate"=>"2005/7/19 22:0:0",
    "status"=>"N"
);
0

2 Answers 2

0

Maybe a quicker way, but off the top of my head:

$x = simplexml_load_string($s);
$a = (array)$x->message->attributes();
$report = $a['@attributes'];
print_r($report);
1
  • Oh great, working as charm. Thanks a lot. Commented Nov 5, 2013 at 20:16
0

Here is one way to do it, it loads the content to the $report array for the $xmlstr value.

$xmlstr = your input xml value.

$xml = simplexml_load_string ( $xmlstr );    

$report = array ();
foreach($xml->message[0]->attributes() as $a => $b) {
    $report[$a] = $b;
}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.