0

Simply put, creating an XML string with array_walk_recursive() appears to place the keys as values and values as keys. What am I doing wrong?

$root = 'Auth';
$auth['user'] = 'JohnSmith';
$auth['password'] = 'somePassword';

$xml = new SimpleXMLElement("<${root}/>");
array_walk_recursive($auth, [$xml, 'addChild']);

echo $xml->asXml();
/**
* <?xml version="1.0"?>
* <Auth>
*     <JohnSmith>user</JohnSmith>
*     <somePassword>password</somePassword>
* </Auth>
*/

There are a number of resources on SO and Google that have this explicit setup as "how it's done" and none of them seem to notice the reversal even though it's there in their dumped output

ggdx
  • 3,024
  • 4
  • 32
  • 48
  • [This](https://stackoverflow.com/a/1397164/1213708) answer seems to mention it at the end. – Nigel Ren Aug 30 '19 at 15:04
  • Fantastic. Thanks @NigelRen, much appreciated. This particular question appears to have escaped my search. – ggdx Aug 30 '19 at 15:10

0 Answers0