Pick key value set with hyphen

Asked

Viewed 468 times

0

I am trying to access the value of the url-tool key, but it presents 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?

1 answer

3


The elements of SimpleXMLElement comply with the variable name conventions of the PHP, and the - is not an allowed character.

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

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

This form can also be used when using the value of variables:

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

See more examples in the online PHP manual.

  • Bacco, thank you for your reply!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.