The quickest way to get an array like the one you want would be to do this:
$dom = simplexml_load_file($yourFile);
$result = array();
foreach ($dom as $node)
{//iterate over elements
if ($node->getName() === 'Name')
{//make sure to only process Name tags
$lang = null;
foreach ($node->attributes() as $name => $val)
{//find the attribtues
if ($name == 'lang')
{//we have the lang attribute
$lang = (string) $val;//cast to string!
break;//we're done here
}
}
if ($lang)
$result[$lang] = (string) $node;//cast node to string to get its contents
}
}
var_dump($result);
The output will be:
array(2) {
["en"]=>
string(6) "Soccer"
["it"]=>
string(6) "Calcio"
}
As OIS suggested in his answer, you could also use SimpleXMLElement::xpath(), which allows you to do away with those ugly nested loops. Apart from the XPath he suggested, I'd probably go for that approach:
foreach ($dom->xpath('//Name[@lang]') as $name)
$result[(string) $name['lang']] = (string) $name;
SimpleXMLElementinstances need to be cast to stringsNames, but you don't need to: eval.in/204444 - but doesnot work is also not a good programming question.