php - 获取 XML 节点名称而不在 simplexml_load_file 中重复
问题描述
我正在使用以下代码获取 XML 的节点名称:
$url = 'https://www.toptanperpa.com/xml.php?c=shopphp&xmlc=e7ef2a0122';
$xml = simplexml_load_file($url) or die("URL Read Error");
echo $xml->getName() . "<br>";
foreach ($xml->children() as $child) {
echo $child->getName() . "<br>";
foreach ($child->children() as $child2) {
echo $child2->getName() . "<br>";
foreach ($child2->children() as $child3) {
echo $child3->getName() . "<br>";
foreach ($child3->children() as $child4) {
echo $child4->getName() . "<br>";
}
}
}
}
我正确地获取了节点和子节点,但是,它是重复的。
结果如下:
urunler
urun
urun_aktif
urun_metaKeywords
urun_metaDescription
urun_url
urun
urun_aktif
urun_metaKeywords
urun_metaDescription
urun_url
我应该使用array_unique
还是有更好的方法?
谢谢
解决方案
我使用了递归函数,这很简单
function getChildrens($x) {
$children = array();
array_push($children, $x->getName());
foreach ($x->children() as $chld) {
$children = array_merge($children, getChildrens($chld));
}
return array_unique($children);
}
echo implode("<br>", getChildrens($xml));
推荐阅读
- angular - 如何完成嵌套的 Observables?
- flutter - “颤振医生”卡住了,颤振命令不起作用
- python - Python 对于任何也暴露 len() 的可迭代对象,正确的类型是什么?
- php - 如何在不使用 setter 的情况下调试更新 php Trait 的实例变量
- python - 如何修复“禁止(未设置 CSRF cookie。)”
- javascript - Chrome 重定向被取消 - 升级不安全请求:1
- python - driver.page_source 使用 Selenium 仅返回 meta name="ROBOTS" content="NOINDEX, NOFOLLOW"
- php - php包含标题在某些机器上不显示
- linux - 操作系统确定内核或某些用户程序的输入
- android - 如何在我的活动中存储来自 API 的数据