我有这个片段(我想从.xml文件中获取元素):
$movies = simplexml_load_file('http://www.example.com/example.xml');
$out = "";
foreach ($movies as $movie) {
$properties = array(
'photo' => $movie->image,
'title' => $movie->title,
'desc' => $movie->teaser,
'channel' => $movie->channel,
'date' => $movie->date);
$out .= $modx->getChunk('tpl_movies-item', $properties);
}
return $out;
和chunk tpl_movies-item:
<article>
[[+photo]]
[[+title]]
[[+desc]]
[[+date]]
[[+channel]]
aaa
</article>
它仅显示&#34; aaa&#34; (但是当我将数组中的值更改为字符串时,例如,当我添加&#34; echo $ properties [&#39; photo&#39;]&#34;它打印正确的值)时&#39;降序&#39; =&GT; &#34; lololololol&#34;它运作正常。你能帮帮我,我该怎么办?
答案 0 :(得分:2)
问题可能出在你的foreach语句中,因为simplexml_load_file()
需要->children()
来遍历手头对象的不同子节点。尝试:
foreach ($movies->children() as $movie) {
$properties = array(
'photo' => (string)$movie->image,
'title' => (string)$movie->title,
'desc' => (string)$movie->teaser,
'channel' => (string)$movie->channel,
'date' => (string)$movie->date);
$out .= $modx->getChunk('tpl_movies-item', $properties);
}
<强>更新强>
尝试将属性转换为string
,如上例所示,因为它们可以作为对象返回。