Redeem value of object

Asked

Viewed 30 times

1

I own an object like this:

SimpleXMLElement Object
(
    [0] => 1bf7080ac1c04e4e9de924c0a7d9444d
)

But I can not only redeem the value of it. I have tried the following ways:

print_r($obj{0});
print_r($obj{'0'});
print_r($obj->{0});
print_r($obj->{'0'});
print_r($obj[0]);
print_r($obj['0']);
print_r($obj->[0]);
print_r($obj->['0']);

But I was unsuccessful!

  • Pq does not convert to an array?

  • @Jorgematheus good, I hadn’t thought about it. That somehow already solves my problem. Thanks! ;) But I would still like to know how to rescue as Object.

  • Try it like this: current($obj). Current points to the current Index, that is, the first.

  • 1

    @Jorgematheus Perfect! Add your comment as reply. It may serve for other people tb! ;)

1 answer

1


You can use the function current that points to the index corrente of array, in that case the index 0.

Would look like this:

current($obj);

Alternatively, you can convert to array:

$xml = simplexml_load_file('seu_arquivo.xml');    
$string = json_encode($xml);    
$array = json_decode($string);

Browser other questions tagged

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