I need to extract some data from a XML fetched online and to create a JSON from the result.
Here is a simplified version of the issue I encounter:
$XML=simplexml_load_file("http://somewhere.on.the.net/my.xml");
$result = array();
$tmpp = $XML->S->S["time"];
echo $tmpp.'<br/>';;
$result['DATE'] = $tmpp;
echo json_encode($result);
I get:
2012-05-29
{"DATE":{"0":"2012-05-29"}}
and I want:
{"DATE":"2012-05-29"}
How can I achieve this? Thanks.
Update
Here is the structure of the XML:
<g:e>
<S>
<S time="2012-05-29">
<S info1="a" info2="b"/>
<S info1="d" info2="m"/>
<S info1="q" info2="l"/>
...
</S>
<S time="2011-04-09">
<S info1="a" info2="z"/>
...
</S>
</S>
...
</S>
</g:e>
Looks like you may need to be getting the string value from the XML node:
It looks like a bare date value when you
echoit because SimpleXML implements a custom__toString()on the object, but if youvar_dump($XML->S->S["time"])you will see there’s more to it than just the date string.