Get key value set with hyphen

0

I'm trying to access the value of the url-authentication key, but it displays the following message:

Notice: Use of undefined constant autenticacao - assumed 'autenticacao' in
SimpleXMLElement Object
(
[@attributes] => Array
(
[versao] => 1.3.0
[id] => 123
)

[tid] => 10017348980132951001
[dados-pedido] => SimpleXMLElement Object
(
[numero] => 123
[valor] => 1000
[moeda] => 986
[data-hora] => 2014-04-30T23:54:07.892-03:00
[descricao] => [origem:::1]
[idioma] => PT
[taxa-embarque] => 0
)

[forma-pagamento] => SimpleXMLElement Object
(
[bandeira] => visa
[produto] => 1
[parcelas] => 1
)

[status] => 0
[url-autenticacao] => https://qasecommerce.cielo.com.br/web/index.cbmp?id=789b9ef9858bf2aba2751df3f306424a
)
Tentei usar o echo $xml->url-autenticacao mas apresentou a mensagem de erro descrito acima. 

How do I get the value of this key?

    
asked by anonymous 01.05.2014 / 13:17

1 answer

3

Elements of SimpleXMLElement obey the PHP variable name conventions, and - is not a permitted character.

In this case, use { and } to encapsulate the name of these elements:

echo $xml->{'url-autenticacao'};

This form can also be used when you want to use the value of variables:

$elemento = 'pipoca';
echo $xml->{$elemento};

See more examples on manual PHP online .

    
01.05.2014 / 13:54