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

2
  • This answer seems to mention it at the end. Commented Aug 30, 2019 at 15:04
  • Fantastic. Thanks @NigelRen, much appreciated. This particular question appears to have escaped my search. Commented Aug 30, 2019 at 15:10

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.