Bug #80856 [NEW]: SimpleXMLElement attributes are ignored when converting to array
From: ondrej dot sibrava at varhall dot cz
Operating system: Any
PHP version: 8.0.3
Package: SimpleXML related
Bug Type: Bug
Bug description:SimpleXMLElement attributes are ignored when converting to array
Description:
------------
When converting SimpleXMLElement to array and nested element has XML
attributes and its content contains only text, output array does not
include '@attributes' key.
This bug has been already fixed in PHP versions 7.3.17 and 7.4.5 (Bug
#61597), but appeared again.
Test script:
---------------
$str = <<<'EOX'
<?xml version="1.0" encoding="UTF-8"?>
<test>
<person>
<name>Pepa</name>
<surname>Novak</surname>
<emails>
<email type="private">pepa@gmail.com</email>
<email type="work">pepa.novak@cmp.com</email>
<email type="work">novak@company.com</email>
</emails>
</person>
</test>
EOX;
$xml = simplexml_load_string($str);
var_dump(json_decode(json_encode($xml), true));
Expected result:
----------------
array(1) {
["person"]=>
array(3) {
["name"]=>
string(4) "Pepa"
["surname"]=>
string(5) "Novak"
["emails"]=>
array(1) {
["email"]=>
array(3) {
[0]=>
array(2) {
["@attributes"]=>
array(1) {
["type"]=>
string(7) "private"
}
[0]=>
string(14) "pepa@gmail.com"
}
[1]=>
array(2) {
["@attributes"]=>
array(1) {
["type"]=>
string(4) "work"
}
[0]=>
string(18) "pepa.novak@cmp.com"
}
[2]=>
array(2) {
["@attributes"]=>
array(1) {
["type"]=>
string(4) "work"
}
[0]=>
string(16) "novak@company.cz"
}
}
}
}
}
Actual result:
--------------
array(1) {
["person"]=>
array(3) {
["name"]=>
string(4) "Pepa"
["surname"]=>
string(5) "Novak"
["emails"]=>
array(1) {
["email"]=>
array(3) {
[0]=>
string(14) "pepa@gmail.com"
[1]=>
string(18) "pepa.novak@cmp.com"
[2]=>
string(16) "novak@company.cz"
}
}
}
}
--
Edit bug report at https://bugs.php.net/bug.php?id=80856&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=80856&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=80856&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=80856&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=80856&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=80856&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=80856&r=support
Expected behavior: https://bugs.php.net/fix.php?id=80856&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=80856&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=80856&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=80856&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=80856&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=80856&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=80856&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=80856&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=80856&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=80856&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=80856&r=mysqlcfg
Thread (3 messages)
- ondrej dot sibrava at varhall dot cz