我正在解析XML提要,並嘗試創建JSON輸出。我似乎無法弄清楚爲什麼我的JSON格式化關閉。這是我通過XML飼料使用循環的代碼,分析它,並建立JSON輸出:使用SimpleXML解析XML提要後出現錯誤的JSON格式化
$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);
}
這是輸出:
[
{
"title":{
"0":"This is my article title"
},
"image":null,
"resource":{
"articleLink":"http://www.website.com/link.html"
}
}
]
然而,這是輸出格式我需要:
[
{
"title":"This is my article title",
"image":null,
"resource":{
"articleLink":"http://www.website.com/link.html"
}
}
]
爲什麼「標題」被添加爲鍵/值對?
沒有看到XML不知道,但你可以修復:'「稱號」 = > $ article-> title [0],' – Steve 2014-11-21 15:15:59