|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #61877 Float keys not cast to int like with array
Submitted: 2012-04-30 02:23 UTC Modified: 2021-08-18 17:08 UTC
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: fred dot stcm at gmail dot com Assigned:
Status: Verified Package: SimpleXML related
PHP Version: 5.4.1 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: fred dot stcm at gmail dot com
New email:
PHP Version: OS:


 [2012-04-30 02:23 UTC] fred dot stcm at gmail dot com
PHP Array manual:
"Floats are also cast to integers, which means that the fractional part will be 
truncated. E.g. the key 8.7 will actually be stored under 8."

But this rule is ignored by SimpleXML object's array.

Test script:
$xml = simplexml_load_string("<xml><number>0</number><number>1</number></xml>");

$i = (int) 1;
$d = (float) 1;

$xml->number[$d] = 999;

echo $xml->number[$d] .", ". $xml->number[$i];

Expected result:
999, 999

Actual result:
999, 1


simplexml-patch (last revision 2012-04-30 20:58 UTC by

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-04-30 20:58 UTC]
The following patch has been added/updated:

Patch Name: simplexml-patch
Revision:   1335819480
 [2020-03-27 01:04 UTC]
The following pull request has been associated:

Patch Name: Fix #61877: Make SimpleXMLElement respect PHP's array rule
On GitHub:
 [2021-08-18 17:08 UTC]
-Summary: Array key not casted to Integer. +Summary: Float keys not cast to int like with array -Status: Open +Status: Verified -Type: Bug +Type: Documentation Problem
 [2021-08-18 17:08 UTC]
> But this rule is ignored by SimpleXML object's array.

There is no SimpleXML object array, to begin with.
SimpleXMLElements are more like classes implementing ArrayAccess,
and ArrayAccess allows arbitrary type behavior regarding the keys.

Anyhow, as it is, SimpleXMLElement array access distinguishes
between integer and non-integer keys; the latter are cast to
string.  These have fundamentally different behavior, namely
integer keys are interpreted as n-th child, while string keys are
interpreted as attribute names.  So casting float keys to integer
would be a BC break, although float keys would create invalid XML
attribute names on write access, anyway.

To move forward with this ticket, I'm going to change to doc
problem; while there is some explanation of array access in the
examples section[1], this doesn't look complete and should better
be documented on the class summay page[2] (or at least linked from
there).  Even better yet, SimpleXML would actually implement
ArrayAccess (see request #52655).

If you, or anybody else, still feels float keys should be cast to
integer, please pursue the RFC process[3].

[1] <>
[2] <>
[3] <>
 [2022-11-29 06:53 UTC] samanre32 at gmail dot com
Thanks for that. (
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 16:01:28 2024 UTC