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"
);
MD SHAHIDUL ISLAM
  • 14,325
  • 6
  • 82
  • 89

2 Answers2

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);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
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;
}
dhunju
  • 71
  • 3