php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27139 can't iterate thru simplexml_element objects in normal ways
Submitted: 2004-02-03 22:16 UTC Modified: 2004-02-04 14:56 UTC
From: kennyt@php.net Assigned:
Status: Not a bug Package: *XML functions
PHP Version: 5CVS-2004-02-03 (dev) OS:
Private report: No CVE-ID: None
 [2004-02-03 22:16 UTC] kennyt@php.net
Description:
------------
We should be able to use normal array iteration 
techniques when going through such pseudoarray 
structures in simplexml elements, right?

Reproduce code:
---------------
$xml = simplexml_load_string(
'<a>
 <b></b>
 <b></b>
 <b></b>
</a>');

while(list($foo, $bar) = each($xml->b)) {
  echo '"' , $foo , '" - "', $bar , '"' , chr(10);
}

--OR--

for($i = 0; $i < count($xml->b); ++$i) {
  echo '"' , $i , '" - "', $xml->b[$i] , '"' , chr(10);
}

Expected result:
----------------
[3 lines of output]

--OR--

"0" - ""
"1" - ""
"2" - ""

Actual result:
--------------
[no output]

--OR--

"0" - ""

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-02-03 23:55 UTC] adam at trachtenberg dot com
No. You need to use foreach() or the iterator magic 
won't work.
 [2004-02-04 02:28 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

As Adam said, use foreach.
 [2004-02-04 08:11 UTC] kennyt@php.net
Okay, does foreach do its usual copy job on the set, or 
is it a simulated foreach? And if it's the former, does 
that really matter? (They're just pointers, right?)
 [2004-02-04 14:56 UTC] helly@php.net
Depends on the actual iterator being executed. For the simpeXML_Element class there will be no copy. And even in the whole ext/SPL there will be no copying.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC