SimpleXMLElement to PHP Array

Codex73 picture Codex73 · Apr 28, 2010 · Viewed 68.1k times · Source

Variable $d comes from file_get_contents function to a URL.

$answer = @new SimpleXMLElement($d);

Below is output of the print_r($answer):

SimpleXMLElement Object
(
  [Amount] => 2698
  [Status] => OK
  [State] => FL
  [Country] => USA
)

How can I retrieve value of each element and add to an array? I can't figure it out.

Answer

dkinzer picture dkinzer · Apr 28, 2010

In this simple case type casting will also work:

$my_array = (array)$answer