Bug #80856 [Opn->Dup]: SimpleXMLElement attributes are ignored when converting to array
| From: | requinix@php.net | Date: | Fri, 12 Mar 2021 07:48:40 +0000 |
| Subject: | Bug #80856 [Opn->Dup]: SimpleXMLElement attributes are ignored when converting to array | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-232666@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=80856&edit=1
ID: 80856
Updated by: requinix@php.net
Reported by: ondrej dot sibrava at varhall dot cz
Summary: SimpleXMLElement attributes are ignored when
converting to array
-Status: Open
+Status: Duplicate
Type: Bug
Package: SimpleXML related
Operating System: Any
PHP Version: 8.0.3
Block user comment: N
Private report: N
New Comment:
61597's fix was reverted and the bug report is open.
Previous Comments:
------------------------------------------------------------------------
[2021-03-12 07:31:05] ondrej dot sibrava at varhall dot cz
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 this bug report at https://bugs.php.net/bug.php?id=80856&edit=1