Bug #80856 [Com]: SimpleXMLElement attributes are ignored when converting to array
| From: | rowan dot collins at gmail dot com | Date: | Fri, 12 Mar 2021 09:01:24 +0000 |
| Subject: | Bug #80856 [Com]: SimpleXMLElement attributes are ignored when converting to array | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-232668@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
Comment by: rowan dot collins at gmail dot com
Reported by: ondrej dot sibrava at varhall dot cz
Summary: SimpleXMLElement attributes are ignored when
converting to array
Status: Duplicate
Type: Bug
Package: SimpleXML related
Operating System: Any
PHP Version: 8.0.3
Block user comment: N
Private report: N
New Comment:
XML and PHP's arrays are fundamentally different data structures. While it is possible to
represent any XML document as a deeply recursive array structure, the resulting format is rarely
particularly useful.
The hack shown converts first to JSON, which introduces a third data model, incompatible with both
XML and PHP's arrays.
Converting to an array is not, and in my opinion should not be, a feature of the SimpleXML
extension. 99 times out of 100, people trying to do this actually just want access to different
parts of the XML structure, which SimpleXML already provides.
Note that this is separate from having a debug representation of the object, which probably should
be fixed.
Previous Comments:
------------------------------------------------------------------------
[2021-03-12 07:48:40] requinix@php.net
61597's fix was reverted and the bug report is open.
------------------------------------------------------------------------
[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