Bad JSON formatting after parsing XML feed using SimpleXML



I'm parsing an XML feed, and attempting to create JSON output. I can't seem to figure out why my JSON formatting is off. This is the code I'm using to loop through the XML feed, parse it, and build JSON output:



$xml = simplexml_load_file($myxmlfeed, 'SimpleXMLElement', LIBXML_NOERROR | LIBXML_NOWARNING);

foreach ($xml->{'xml-node-name'} as $article)
{
$tmp = array(
"title" => $article->title,
"image" => null,
"resource" => array(
"articleLink" => $site)
);
array_push($array, $tmp);
unset($tmp);
}


This is the output:



[
{
"title":{
"0":"This is my article title"
},
"image":null,
"resource":{
"articleLink":"http://ift.tt/11KuRX0"
}
}
]


However, this is the output format I need:



[
{
"title":"This is my article title",
"image":null,
"resource":{
"articleLink":"http://ift.tt/11KuRX0"
}
}
]


Why is the "title" being added as a key/value pair?


No comments:

Post a Comment