【发布时间】:2015-04-24 17:31:28
【问题描述】:
我的问题是我无法访问 JSON 数组中的特定对象。我遵循了很多解决方案,但它们不起作用! 这是我的脚本:
<?php
class XmlToJsonConverter {
public function ParseXML ($url) {
$fileContents= file_get_contents($url);
// Remove tabs, newline, whitespaces in the content array
$fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);
$fileContents = trim(str_replace('"', "'", $fileContents));
$myXml = simplexml_load_string($fileContents);
//--------------
unset($myXml['@attributes']);
unset($myXml['channel']);
//$json = json_encode($myXml['item']);
//var_dump($myXml);
$json = json_encode($myXml);
$arr = json_decode($json, true);
echo json_encode($arr['item']);
}
}
//Path of the XML file
$url= 'http://www.lequipe.fr/rss/actu_rss_Football.xml';
//Create object of the class
$jsonObj = new XmlToJsonConverter();
//Pass the xml document to the class function $myjson =
$myjson = $jsonObj->ParseXMl($url);
//echo ($myjson);
?>
此脚本返回 null。 我只想访问这个数组中的“项目”。
感谢您的帮助。
【问题讨论】:
-
@MarcB:感谢您的指出。