The simpleXml behavior for arrays looks strange to me and - breaks at 9941 positions

EDIT3: It seems that the problem occurs on my localhost XAMPP PHP 5.3 installation, and not on any remote server running php 5.2 that I tested. It is still unclear whether its php or xampp (or maybe a combination) is causing the / EDIT 3 error

I have xml with about 12000 names to add to the array. The xml structure is as follows:

<smdusersync datetime="2010-12-13 13:51:16"> <userstoadd> <User fnamn="Adam" enamn="Svensson" email=" adam@darkeye.se " password="3906" /> <User fnamn="Brooke" enamn="Jarnbjer" email=" brooke@gmail.com " password="2729" /> <User fnamn="Caesar" enamn="Carlsson" email=" caesar@comhem.se " password="1668" /> <!-- about 12000 other users --> </userstoadd> <userstoremove> </userstoremove> </smdusersync> 

EDIT2: I tried with other xml examples, including programmatically generated without attbutes, etc., and it doesn’t matter - the same problem is described below ... / EDIT 2

When you start a simple foreach loop in the xml userstoadd file, strange things happen when I click objects on an array.

(Please note that the example below shows the code causing the error - this is not a very useful example)

Running a simple foreach loop (here just pushing testcounter into an array) works as expected:

  $user_xml = simplexml_load_file('users.xml'); $xml_count = $user_xml->userstoadd->children()->count(); $users_arr = array(); $test_count = 0; foreach ($user_xml->userstoadd->children() as $user) { array_push($users_arr, $test_count); // << Works as expected! $test_count++; } echo $xml_count, '/', $test_count; 

$ xml_count and $ test_count always have the same value.

When I do the same, besides this, I click on a simple object on an array, everything works fine if the number of users xml <= 9940:

  $user_xml = simplexml_load_file('users.xml'); $xml_count = $user_xml->userstoadd->children()->count(); $users_arr = array(); $test_count = 0; foreach ($user_xml->userstoadd->children() as $user) { $dummy_object = new StdClass(); // << Empty dummy object array_push($users_arr, $dummy_object); // << CAUSES ERROR if more than 9940 items...! $test_count++; } echo $xml_count, '/', $test_count; // << SHOULD BE THE SAME! 

With 9940 xml custom elements, the result is expected to be 9940/9940 . BUT in the presence of 9941 xml of user elements an exit 9941/19881 ! And having 9942 xml custom elements, exit 9942/19882!

Suddenly almost 10,000 difference! And the content of the user elements does not seem to matter ... I copied and moved the xml user elements with the same result ...

EDIT: When more than 9940 items, it suddenly doubles, so 9941 points gives (9940 x 2) + 1 = 19881. There is no difference when using one xml user element 12000 times. / EDIT

Any idea what is going on here?

+4
source share
3 answers

The following madness solves the problem for me:

You might want to comment out the line test(50000); .

 <?php test(9996); // works (echoes 9996/9996/9996) test(9997); // BREAKS on my XAMPP php 5.3.1! (echoes 9997/19993/19993) test(50000); // test with larger number function test($items_to_create) { $xml_str = '<root>' . chr(13); for($i=0; $i<$items_to_create; $i++) { $xml_str .= '<item attr="t' . $i . '"/>' . chr(13); } $xml_str .= '</root>'; $xml = new SimpleXMLElement($xml_str); $xml_children = $xml->children(); $xml_count = $xml_children->count(); $arr = array(); $test_count = 0; /* foreach ($xml->children() as $user) { $dummy_object = new StdClass(); array_push($arr, $dummy_object); $test_count++; } */ for ($i=0; $i<$xml_count; $i++) { $dummy_object = new StdClass(); $dummy_object->foo = $xml_children[$i]->attributes()->attr; array_push($arr, $dummy_object); $test_count++; } $arr_count = count($arr); echo '<br />CTRL+F for me:', $xml_count, '/', $test_count , '/', $arr_count, '<hr />'; echo '<pre>', print_r($arr, true), '</pre>'; } ?> 

Does he fix it for you?

+2
source

As you describe, I do not see any relation to XML processing. Please run this code to check:

 $xml_count = 9950; $users_arr = array(); $test_count = 0; foreach (range(0, $xml_count) as $user) { $dummy_object = new StdClass(); array_push($users_arr, $dummy_object); $test_count++; } echo $xml_count, '/', $test_count, '/', count($users_arr); 

He should print number 9950 three times. If this is not the case on your machine, your PHP is not working.

+1
source

Ok, I highlighted something related to my XAMPP php 5.3 setup. Testing on two different remote servers with php 5.2 and there it works as expected.

The following code breaks my XAMPP into php 5.3.1

 <?php test(9996); // works (echoes 9996/9996/9996) test(9997); // BREAKS on my XAMPP php 5.3.1! (echoes 9997/19993/19993) function test($items_to_create) { $xml_str = '<root>' . chr(13); for($i=0; $i<$items_to_create; $i++) { $xml_str .= '<item />' . chr(13); } $xml_str .= '</root>'; $xml = new SimpleXMLElement($xml_str); $xml_count = $xml->children()->count(); $arr = array(); $test_count = 0; foreach ($xml->children() as $user) { $dummy_object = new StdClass(); array_push($arr, $dummy_object); $test_count++; } $arr_count = count($arr); echo "<br/>", $xml_count, '/', $test_count , '/', $arr_count; } 
+1
source

All Articles