A
A
Alexander Zemlyanoy2014-11-15 10:26:36
PHP
Alexander Zemlyanoy, 2014-11-15 10:26:36

How to access a field of a PHP object if the name consists of numbers?

Hello php progers.
I came across such a problem, after parsing the xml string simplexml_load_string I get such an object prntscr.com/56iw04 how to access properties with such names.
Tried like this

$xml->url[0];
$xml->url['0'];
$xml->url->0;
$xml->url->'0';
$xml->url->{'0'};
$xml->url->{0};

There are 3 cases either NULL or an empty object or an error.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Zemlyanoy, 2014-11-15
@Galamoon

All the task is solved it is necessary to explicitly cast the types (array)$xml->url[0];
I did not know that in php you can cast to an array)))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question