In this simple case type casting will also work:

$my_array = (array)$answer
Answer from david on Stack Overflow
🌐
GitHub
gist.github.com › jasondmoss › 7344311
Convert a SimpleXML object to associative array · GitHub
*/ public static function simpleXmlToArray(?SimpleXMLElement $xml): array|string { if ($xml === null) { return []; } $result = []; foreach ($xml->attributes() as $attrName => $attrValue) { $result['@attributes'][$attrName] = (string) $attrValue; } foreach ($xml->children() as $childName => $childElement) { $childArray = self::simpleXmlToArray($childElement); if (!isset($result[$childName])) { $result[$childName] = $childArray; continue; } if (!is_array($result[$childName]) || !array_is_list($result[$childName])) { $result[$childName] = [$result[$childName]]; } $result[$childName][] = $childArray; } $textContent = trim((string) $xml); if (empty($result)) { return $textContent; } if ($textContent !== '') { $result['@value'] = $textContent; } return $result; }
🌐
PHP
php.net › manual › en › class.simplexmlelement.php
PHP: SimpleXMLElement - Manual
Map xml to array (with attributes) <?php declare(strict_types=1); /** * @param SimpleXMLElement $xml * @return array */ function xmlToArray(SimpleXMLElement $xml): array { $parser = function (SimpleXMLElement $xml, array $collection = []) use (&$parser) { $nodes = $xml->children(); $attributes = $xml->attributes(); if (0 !== count($attributes)) { foreach ($attributes as $attrName => $attrValue) { $collection['attributes'][$attrName] = strval($attrValue); } } if (0 === $nodes->count()) { $collection['value'] = strval($xml); return $collection; } foreach ($nodes as $nodeName => $nodeValue) { if (count($nodeValue->xpath('../' .
🌐
SitePoint
sitepoint.com › php
How can I get values in array from simpleXML object? - PHP - SitePoint Forums | Web Development & Design Community
August 10, 2010 - Hi, Got a simplexml object as follows: [Attributes] => SimpleXMLElement Object ( [key1] => value1 [key2] => value1 [key3] => value1 [list1] => Array
🌐
Bookofzeus
bookofzeus.com › articles › php › convert-simplexml-object-into-php-array
Convert SimpleXML Object into PHP Array | Book of Zeus
xml2array($element) : $e; } else { $arr[$tag] = trim($element); } } return $arr; } $xml = new SimpleXMLElement($string); $xml = json_decode(json_encode((array) simplexml_load_string($string)), 1);
🌐
Shanestillwell
shanestillwell.com › php-simplexmlelement-values-into-an-array
PHP SimpleXmlElement values into an array - Shane A. Stillwell
<php> $xmlObj = new SimpleXMLElement($xml); $item = $xmlObj->children('ns0', TRUE); $data = array(); $data['Style'] = (string)$item->Style; $data['Status'] = (string)$item->Status; $data['Qty_Avail'] = (string)$item->Quantity_Available; $data['Price'] = (string)$item->Wholesale_Price; </php> Now I get a nice array, like I was expecting.
🌐
GitHub
gist.github.com › yeroon › 925483
Converts a simpleXML element into an array. Preserves attributes and everything. · GitHub
Converts a simpleXML element into an array. Preserves attributes and everything. - simpleXmlToArray
Find elsewhere
🌐
BinaryTides
binarytides.com › home › convert simplexml object to array in php
Convert simplexml object to array in php - BinaryTides
December 6, 2011 - 1. count($r->children()) == 0 - If the children count of a simplexmlelement object is 0 it can be taken as an empty node and shown as a string in the array. 2. $arr[$r->getName()] = strval($r) - $r is a Simplexmlelement object and to get its ...
🌐
SitePoint
sitepoint.com › php
SimpleXMLElement array - PHP - SitePoint Forums | Web Development & Design Community
January 10, 2012 - Good day, This is the output of a var_dump. I would like to know if someone has an idea on how I can get the value contain in the attribues contained in $self ? thanks. Code: $self = $xml->xpath(‘/rss/channel/atom:link[@rel=\‘self\’]’); var_dump($self); Output: array 0 => object(SimpleXMLElement)[2] public ‘@attributes’ => array ‘rel’ => string ‘self’ (length=4) ‘type’ => string ‘application/rss+xml’ (length=19) ‘href’ => string ‘http://beta.geogratis.gc.ca/api/en/nrcan-rncan/ess-s...
🌐
Alun Rowe
alunr.com › home › turn a simplexml object into a php array
Turn a SimpleXML object into a PHP array - Alun Rowe
February 10, 2016 - Nice simple quick trick but makes dealing with SimpleXML so much easier!
🌐
GitHub
gist.github.com › hakre › 4012538
Convert/Import an Array into a SimpleXMLElement · GitHub
Save hakre/4012538 to your computer and use it in GitHub Desktop. Download ZIP · Convert/Import an Array into a SimpleXMLElement · Raw · array-to-simplexml.php · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below.
🌐
The Art of Web
the-art-of-web.com › php › sort-simplexml
Sorting SimpleXMLElement Object arrays | The Art of Web
Instructions for converting a SimpleXMLElement object into a sortable array.
🌐
SitePoint
sitepoint.com › php
Simplexml - get numeric key for an array - PHP - SitePoint Forums | Web Development & Design Community
January 25, 2010 - Hello, I am using simplexml and cannot find what I think would be a simple solution. How do I get the numeric array key for each <item>? Please see the simple example below… My xml: <data> <artist>Chris Navarro</a…