php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27288 Count returns 1 for array with simple xml
Submitted: 2004-02-17 03:50 UTC Modified: 2004-02-17 06:29 UTC
From: marcels at korton dot nl Assigned:
Status: Not a bug Package: XML related
PHP Version: 5.0.0b4 (beta4) OS: Windows 2000 SP3
Private report: No CVE-ID: None
 [2004-02-17 03:50 UTC] marcels at korton dot nl
Description:
------------
When I use the count function to get the number of elements in an array it returns 1. It does this only when I use simpleXML. In version 5.0.0b3 it worked fine. 

My example use a book in the xml format. Here is a small part of it

<bookbody>
      <part>
        <chapter>
          <chapheader>
            <chapnum>I</chapnum>
            <title>The Period</title>
          </chapheader>
        </chapter>
        <chapter>
          <chapheader>
            <chapnum>II</chapnum>
            <title>The Mail</title>
        </chapheader>

This continues for al the chapters within the book.

both part and chapter are array's.

We are using the windows precompiled binaries with Sambar server V6.0 build 11 november 2003


Reproduce code:
---------------
$xml_mem = simplexml_load_file('2city11.xml');

	for ($i = 0; $i < count($xml_mem->book->bookbody->part); $i++) {
		for ($j = 0; $j < count($xml_mem->book->bookbody->part[$i]->chapter); $j++) {
			echo $xml_mem->book->bookbody->part[$i]->chapter[$j]->chapheader->title,'<br>';
		}
	}


Expected result:
----------------
The Period
The Mail
The Night Shadows
The Preparation
The Wine-shop
The Shoemaker
Five Years Later
A Sight
A Disappointment

...


Actual result:
--------------
The Period

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-02-17 06:11 UTC] sniper@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc.

If possible, make the script source available online and provide
an URL to it here. Try avoid embedding huge scripts into the report.


 [2004-02-17 06:29 UTC] rrichards@php.net
Arrays are no longer returned. The objects implement iterators so use foreach, ie:

foreach ($xml_mem->book->bookbody->part as $part) {
   foreach($part->chapter as $chapter) {
      echo $chapter->chapheader->title.'<br>';
   }
}
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Mon Nov 23 20:01:23 2020 UTC