php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80856 SimpleXMLElement attributes are ignored when converting to array
Submitted: 2021-03-12 07:31 UTC Modified: 2021-03-12 07:48 UTC
From: ondrej dot sibrava at varhall dot cz Assigned:
Status: Duplicate Package: SimpleXML related
PHP Version: 8.0.3 OS: Any
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: ondrej dot sibrava at varhall dot cz
New email:
PHP Version: OS:

 

 [2021-03-12 07:31 UTC] 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"
      }
    }
  }
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-03-12 07:48 UTC] requinix@php.net
-Status: Open +Status: Duplicate
 [2021-03-12 07:48 UTC] requinix@php.net
61597's fix was reverted and the bug report is open.
 [2021-03-12 09:01 UTC] rowan dot collins at gmail dot com
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC