Reputation: 59576
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>
Upvotes: 1
Views: 44
Reputation: 270609
Looks like you may need to be getting the string value from the XML node:
// Cast the value to a string.
$tmpp = (string)$XML->S->S["time"];
It looks like a bare date value when you echo
it because SimpleXML implements a custom __toString()
on the object, but if you var_dump($XML->S->S["time"])
you will see there's more to it than just the date string.
Upvotes: 3
Reputation: 911
Try using the first key for the $tmpp variable.
$result['DATE'] = $tmpp[0];
Upvotes: 0