|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64813 SimpleXMLIterator unintentionally rewinds to beginning
Submitted: 2013-05-10 17:24 UTC Modified: 2021-04-14 13:35 UTC
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: amfriedman at gmail dot com Assigned:
Status: Duplicate Package: SimpleXML related
PHP Version: 5.3.25 OS: Unbuntu 11
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: amfriedman at gmail dot com
New email:
PHP Version: OS:


 [2013-05-10 17:24 UTC] amfriedman at gmail dot com
From manual page:

When under a for-loop and iterating through a SimpleXMLIterator object, merely 
reading from a variable that stores the children being iterated will cause a 
rewind to the beginning of the object within which you are looping.

Test script:
$xml = new SimpleXMLIterator($xml_str);

$nodes = $nodes_for_lookup = $xml->children();

$n_node = -1;
for($nodes->rewind(); $nodes->valid(); $nodes->next()) {
  $node = $nodes->current();

  // This rewinds this loop
  $value_to_check = $nodes[$n_node+1];
  // This also rewinds the loop
  $value_to_check = $nodes_for_lookup[$n_node+1];

  // This does NOT rewind the loop (expected behavior)
  $nodes_now = $xml->children();
  $value_to_check = $nodes_now[$n_node+1];

Expected result:
The loop should NOT be rewound!  Merely reading from the object should not affect 
the iterator's pointer whatsoever.

Actual result:
The loop is getting rewound due to reading a specific key-based item in the 
iterator object/array.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2021-04-14 13:35 UTC]
-Status: Open +Status: Duplicate
 [2021-04-14 13:35 UTC]
Duplicate of
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Jun 23 08:01:28 2024 UTC