Php 在simplexml\u load\u文件中获取XML节点名而不复制
我使用以下代码获取XML的节点名称:Php 在simplexml\u load\u文件中获取XML节点名而不复制,php,xml,duplicates,nodes,Php,Xml,Duplicates,Nodes,我使用以下代码获取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
$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>";
}
}
}
}
我应该使用array\u 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));
函数getChildrens($x){
$children=array();
数组_push($children,$x->getName());
foreach($x->children()作为$chld){
$children=array_merge($children,getChildrens($chld));
}
返回数组_unique($children);
}
echo内爆(“”,getChildrens($xml));
getChildrens
返回元素的唯一名称数组
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));