Its easy with the below package, Just install the package via composer :

https://github.com/spatie/array-to-xml

The implementation will be :

$array= 'invoice' => [
    'date' => date('Y-m-d H:i:s'),
    'due_date' => date('Y-m-d H:i:s'),
    'client' => ['name' => 'John', 'code' => ''],
    'items' => [
        'item' => [
            'name' => 'reg1',
            'description' => 'reg1 on evt',
            'unit_price' => '10',
            'quantity' => '1.0'
        ],
        'item' => [
            'name' => 'reg1',
            'description' => 'reg1 on evt',
            'unit_price' => '10',
            'quantity' => '1.0'
        ]
    ]
]

And then :

$result = ArrayToXml::convert($array);

Don't forget to include use Spatie\ArrayToXml\ArrayToXml; at the top of the class/Controller.

Answer from Gammer on Stack Overflow
🌐
GitHub
github.com › spatie › array-to-xml
GitHub - spatie/array-to-xml: A simple class to convert an array to xml · GitHub
The root element name can then be set using the rootElementName key. $result = ArrayToXml::convert($array, [ 'rootElementName' => 'helloyouluckypeople', '_attributes' => [ 'xmlns' => 'https://github.com/spatie/array-to-xml', ], ], true, 'UTF-8');
Starred by 1.2K users
Forked by 214 users
Languages   PHP
🌐
Packagist
packagist.org › packages › spatie › array-to-xml
spatie/array-to-xml - Packagist
January 12, 2026 - To add attributes to the root element ... the rootElementName key. $result = ArrayToXml::convert($array, [ 'rootElementName' => 'helloyouluckypeople', '_attributes' => [ 'xmlns' => 'https://github.com/spatie/array-to-xml', ], ], true, 'UTF-8');...
🌐
GitHub
github.com › spatie › array-to-xml › blob › main › README.md
array-to-xml/README.md at main · spatie/array-to-xml
You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall. You can install this package via composer. ... use Spatie\ArrayToXml\ArrayToXml; ...
Author   spatie
🌐
GitHub
github.com › spatie › array-to-xml › blob › main › src › ArrayToXml.php
array-to-xml/src/ArrayToXml.php at main · spatie/array-to-xml
A simple class to convert an array to xml. Contribute to spatie/array-to-xml development by creating an account on GitHub.
Author   spatie
🌐
Stack Overflow
stackoverflow.com › questions › 41020242 › convert-complexe-arrays-to-xml-laravel
Convert Complexe arrays to XML Laravel - Stack Overflow
I'm building a Web Service where ... it to the user as Xml. when there is only one array i can do it using this bundle spatie\array-to-xml\ ......
🌐
GitHub
github.com › spatie › array-to-xml › blob › main › CHANGELOG.md
array-to-xml/CHANGELOG.md at main · spatie/array-to-xml
A simple class to convert an array to xml. Contribute to spatie/array-to-xml development by creating an account on GitHub.
Author   spatie
🌐
GitHub
github.com › spatie › array-to-xml › releases
Releases · spatie/array-to-xml
A simple class to convert an array to xml. Contribute to spatie/array-to-xml development by creating an account on GitHub.
Author   spatie
Find elsewhere
🌐
GitHub
github.com › spatie › array-to-xml › discussions › 191
XML to array · spatie/array-to-xml · Discussion #191
I suspect I am missing something obvious seeing as I thought there would be others asking about this feature... I really like the Spatie conventions used to convert array to XML (eg. _attributes, _...
Author   spatie
🌐
GitHub
github.com › spatie › array-to-xml › watchers
Watchers · spatie/array-to-xml
A simple class to convert an array to xml. Contribute to spatie/array-to-xml development by creating an account on GitHub.
Author   spatie
🌐
Scrutinizer-ci
scrutinizer-ci.com › g › spatie › array-to-xml
Code Quality Summary - spatie/array-to-xml - Measure and Improve Code Quality continuously with Scrutinizer
Please ask an admin of your repository to re-new the access token on this website. ... Inspected Add support for dropping XML d... ... Inspected Add support for custom keys (#... ... Inspected Feature/102 DOM Properties (#1... ... Inspected Fixes #88: Adds numeric array...
🌐
Packagist
packagist.org › packages › dmb › xml-converter
dmb/xml-converter - Packagist
A package that allows you to easily convert your XML string into array format or generate an XML string from an array. github.com/davidemariabusi/xml-converter · Homepage · Source · Issues · Installs: 314 · Dependents: 0 · Suggesters: 0 · Security: 0 · Stars: 0 · Watchers: 1 · Forks: ...
🌐
Augsburg
go.augsburg.edu › includes › vendor › spatie › array-to-xml
Index of /includes/vendor/spatie/array-to-xml
Index of /includes/vendor/spatie/array-to-xml · Name Last modified Size Description · Parent Directory - README.md 2021-03-06 12:09 12K src/ 2021-03-06 12:09 - · Apache Server at go.augsburg.edu Port 80
🌐
Packagist
packagist.org › packages › aivec › array-to-xml
aivec/array-to-xml - Packagist.org
March 4, 2020 - This package provides a very simple class to convert an array to an xml string. This is a fork of https://github.com/spatie/array-to-xml simply for compatibility with older versions of PHP.
🌐
GitHub
github.com › spatie › array-to-xml › network
Network Graph · spatie/array-to-xml
A simple class to convert an array to xml. Contribute to spatie/array-to-xml development by creating an account on GitHub.
Author   spatie
🌐
Iamrohit
iamrohit.in › convert-array-xml-using-php
How to Convert an array to xml using PHP – iamrohit.in
December 20, 2017 - Bellow is the sample example to convert array to XML. use Spatie\ArrayToXml\ArrayToXml; $array = [ 'Good guy' => [ 'name' => 'Luke Skywalker', 'weapon' => 'Lightsaber' ], 'Bad guy' => [ 'name' => 'Sauron', 'weapon' => 'Evil Eye' ] ]; $result ...
🌐
Laravelshift
laravelshift.com › can-i-upgrade-laravel › spatie › array-to-xml
Laravel Shift - Can I upgrade Laravel - spatie/array-to-xml
Check the compatibility of spatie/array-to-xml with the latest versions of Laravel to determine if you may upgrade Laravel or which Composer constraints to use.