Reputation: 1040
This is my code,
$row=array(username=>'username',password=>'password');
$var=array_flip($row);
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($var, array ($xml, 'addChild'));
$result= $xml->asXML();
If username and password are different say 'abcd' & 'efgh' respectively, xml looks like this:
<root>
<username>abcd</username>
<password>efgh</password>
</root>
but if they are same say 'abcd', xml looks like this:
<root>
<password>abcd</password>
</root>
and I want that it should show like
<root>
<username>abcd</username>
<password>abcd</password>
</root>
So how can I solve that?
Upvotes: 0
Views: 398
Reputation: 46
http://www.php.net/manual/en/function.array-flip.php
If a value has several occurrences, the latest key will be used as its values, and all others will be lost.
if password equals to username, then array_flip returns array with just one element
array_flip(array(username=>'abcd',password=>'abcd'))
// will return array('abcd'=>'password')
you cat use foreach instead of array_walk_recursive in this case
foreach ($row as $k => $v)
$xml->addChild($k, $v);
Upvotes: 1
Reputation: 5683
Try
array_walk_recursive($row, function ($value, $key) use ($xml) {
$xml->addChild($key, $value);
}
);
Upvotes: 0