This is what I got. And this is the solution which I expected.

http://outlandish.com/blog/xml-to-json/

Answer from Abhishek Sanghvi on Stack Overflow
🌐
PHP
php.net › manual › en › simplexmlelement.attributes.php
PHP: SimpleXMLElement::attributes - Manual
Easiest and safest way to get attributes as an array is to use the iterator_to_array function (see http://php.net/manual/en/function.iterator-to-array.php): <?php $x = new SimpleXMLElement('<div class="myclass" id="myid"/>'); $attributes = ...
🌐
Envato Tuts+
code.tutsplus.com › parse-xml-to-an-array-in-php-with-simplexml--cms-35529t
Parse XML to an Array in PHP With SimpleXML | Envato Tuts+
Read these tutorials to learn how to work with data in XML format. Use XML to manipulate data, and learn how to parse and validate XML documents. ... In this post, you'll learn how to parse XML into an array in PHP.
🌐
Adsar
adsar.co.uk › xml-to-array-php
XML to Arrays in PHP - adsar
There's a great function that I use all the time to help read in XML data and convert it to PHP arrays: function xml2array($xml, $flattenValues=true, $flattenAttributes = true, $flattenChildren=true, $valueKey='@value', $attributesKey='@attributes', $childrenKey='@children') { $return = array(); ...
🌐
Medium
medium.com › unhandled-code › converting-attributes-of-an-xml-object-to-an-array-adf2f6377e95
PHP : Converting attributes of an XML object to an Array | by Unhandled Perfection | Unhandled —Dev | Medium
February 9, 2018 - $xmlObject = new SimpleXMLElement($xmlString); $xmlAttributes = $xmlObject->attributes();// current() method takes care of the conversion $xmlAttributesArray = current($xmlAttributes); [ "name" => "AXX" "slug" => "axx" ] PHP · Xml · Php ...
Top answer
1 of 2
1

Ok, I finally found function, that converts XML to object, preserving namespaces, attributes, childs and so on:

function xmlObjToArr($obj) { 
        $namespace = $obj->getDocNamespaces(true); 
        $namespace[NULL] = NULL; 

        $children = array(); 
        $attributes = array(); 
        $name = strtolower((string)$obj->getName()); 

        $text = trim((string)$obj); 
        if( strlen($text) <= 0 ) { 
            $text = NULL; 
        } 

        // get info for all namespaces 
        if(is_object($obj)) { 
            foreach( $namespace as nsUrl ) { 
                // atributes 
                $objAttributes = $obj->attributes($ns, true); 
                foreach( $objAttributes as $attributeName => $attributeValue ) { 
                    $attribName = strtolower(trim((string)$attributeName)); 
                    $attribVal = trim((string)$attributeValue); 
                    if (!empty($ns)) { 
                        $attribName = attribName; 
                    } 
                    $attributes[$attribName] = $attribVal; 
                } 

                // children 
                $objChildren = $obj->children($ns, true); 
                foreach( $objChildren as $childName=>$child ) { 
                    $childName = strtolower((string)$childName); 
                    if( !empty($ns) ) { 
                        $childName = childName; 
                    } 
                    $children[$childName][] = xmlObjToArr($child); 
                } 
            } 
        } 

        return array( 
            'name'=>$name, 
            'text'=>$text, 
            'attributes'=>$attributes, 
            'children'=>$children 
        ); 
    } 
2 of 2
0

I think you have fundamentally the wrong approach. Rather than trying to create one function that can express all possible contents of an XML document in one array, use the APIs provided by SimpleXML to extract the data you need for your particular task.

Given your example input, a useful output might be an associative array of key-value pairs, like this:

Array (
    [Surname] => Ярош
    [Name] => Анна
    [Middle name] => Григорьевна
    [Position] => Торговый представитель розничных продаж
    [City] => BAIKALSEA Company Иркутск
    [Division] => Отдел продаж
    [Department] => Продажи
    [Email] => [email protected]
    [MobPhone] => 79149274726
    [WorkPhone] => -
    [Manager] => Нет
    [HonoredWorker] => Нет
    [Login] => [email protected]
    [Character] => Пользователь 
)

Which can be achieved with a simple loop over a SimpleXML element:

$sx = simplexml_load_string(parsed_attributes = [];
foreach ( $sx->attribute as $attribute_element ) {
    $parsed_attributes[ (string)$attribute_element['name'] ] = (string)$attribute_element;
}

print_r($parsed_attributes);

If you don't know in this part of the application how you're going to use the data, keep it as a SimpleXMLElement, and give the rest of your code the power of that API. If you want to store it to use later, use ->asXML() and store it back in XML form.

Find elsewhere
🌐
Digital Point
forums.digitalpoint.com › threads › php-xml-to-array-with-attributes-and-values.2731076
PHP XML to Array with Attributes and Values
I have an XML which have attributes as well as values in them. I want to convert it to an Array or Array Object along with attributes and values. XML...
🌐
GitHub
github.com › gaarf › XML-string-to-PHP-array
GitHub - gaarf/XML-string-to-PHP-array: One common need when working in PHP is a way to convert an XML document into a serializable array. If you ever tried to serialize() and then unserialize() a SimpleXML or DOMDocument object, you know what I’m talking about.
Here is the result for our sample XML, eg if we print_r($a): Array ( [show] => Array ( [@attributes] => Array ( [name] => Family Guy ) [dog] => Brian [kid] => Array ( [0] => Chris [1] => Meg ) ) )
Starred by 113 users
Forked by 29 users
Languages   PHP 100.0% | PHP 100.0%
🌐
W3Schools
w3schools.com › php › func_xml_parse_into_struct.asp
PHP xml_parse_into_struct() Function
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
🌐
Bin-co
bin-co.com › php › scripts › xml2array
xml2array() - XML Parser for PHP
$repeated_tag_index = array();//Multiple tags with same name will be turned into an array foreach($xml_values as $data) { unset($attributes,$value);//Remove existing values, or there will be trouble //This command will extract these variables into the foreach scope // tag(string), type(string), level(int), attributes(array)....
🌐
GitHub
github.com › multidimension-al › xml-array
GitHub - multidimension-al/xml-array: Creates a PHP array from an XML input and converts attributes.
February 19, 2019 - This library utilizes PSR-4 autoloading, so make sure you include the library near the top of your class file: ... $xml = '<tag>Value</tag>'; $array = XMLArray::generateArray($xml); print_r($array); //$array = array("tag" => array(0 => 'Value')); By ...
Author   multidimension-al
🌐
Stack Overflow
stackoverflow.com › questions › 40767877 › xml-attributes-to-arrays-php
XML Attributes to arrays (PHP) - Stack Overflow
Like if i create arrays from [0] ...th('/idspace/Class') as $products){ foreach($products->attributes() as $a => $b){ if(!isset($$a) $$a=array(); ${$a}[(integer)$products['ClassID']]=$b; }}...
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-convert-xml-file-into-array-in-php
How to convert XML file into array in PHP? - GeeksforGeeks
July 11, 2025 - json_decode() function: The json_decode() function is used to decode a JSON string. It converts a JSON encoded string into a PHP variable. Step 1: Creating an XML file (Optional): Create an XML file which need to convert into the array.
🌐
Envato Tuts+
code.tutsplus.com › tutorials › parse-xml-to-an-array-in-php-with-simplexml--cms-35529
Parse XML to an Array in PHP With SimpleXML - Code
December 8, 2020 - Read these tutorials to learn how to work with data in XML format. Use XML to manipulate data, and learn how to parse and validate XML documents. ... In this post, you'll learn how to parse XML into an array in PHP.