Getting XML node names without duplicating in simplexml_load_file

I’m getting the node names of an XML using this code:

$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>";
                }
            }
        }
    }

I’m getting the nodes and children correctly, however, it’s duplicating.

Result is as below:

urunler
urun
urun_aktif
urun_metaKeywords
urun_metaDescription
urun_url
urun
urun_aktif
urun_metaKeywords
urun_metaDescription
urun_url

Should I just use array_unique or is there a better method?

Thanks

Answer

i used recursive function this is simple

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));