我有和对象数组,如下所示。我无法添加到该对象数组,因为我一直遇到错误。现在是这样的:
SimpleXMLElement Object
(
[url] => Array
(
[0] => SimpleXMLElement Object
(
[loc] => http://jbsoftware.co.uk/
[lastmod] => 2015-02-02
[changefreq] => monthly
[priority] => 1.0
)
)
)
现在添加到此结束,我正在执行以下操作:
$note="
<url>
<loc>{$actual_link}</loc>
<lastmod>{$date}</lastmod>
<changefreq>monthly</changefreq>
<priority>1.0</priority>
</url>
";
$sxe = new SimpleXMLElement($note);
$page[] = $sxe;
这反过来给我提供了这个错误...。
Fatal error: controller::generateSitemap() [<a href='controller.generatesitemap'>controller.generatesitemap</a>]: Cannot create unnamed attribute
谁能让我知道为什么我不能简单地将其添加到当前对象数组的末尾吗?这真的让我感到难过。
$page
不是数组(或者什么都不表示数组对象),而是对象(类的实例)。因此,您不能将其与数组方法一起使用。您只能使用可用的simpleXMLElement类方法。
对于您的特定需求,simpleXMLElement不提供任何方法来将其他simpleXMLElement实例作为子项追加。但是,您可以使用该addChild
方法按元素构建子树:
$url = $page->addChild('url');
$loc = $url->addChild('loc', "{$actual_link}");
$lastmod = $url->addChild('lastmod', "{$date}");
...
$url
,$loc
并$lastmod
有新的SimpleXMLElement实例。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句