EDIT3: It seems that the problem occurs on my localhost XAMPP PHP 5.3 setup, not on any of the remote servers running php 5.2 that I’ve tested. Still unclear if its php or xampp (or maybe the combination) that causes the error /EDIT3
I have an xml with about 12000 names to add to an array.
The xml structure looks like this:
<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’ve tried with other xml examples, including programmatically generated with no attbutes etc, and that doesn’t matter – still the same problem described below… /EDIT2
When running a simple foreach loop on the xml userstoadd child, strange things start to
happen when I push objects to an array.
(Please note that the example below has the code that causes the error – it’s not a useful example in any way…)
Running a simple foreach loop (here just pushing a testcounter to the 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;
The $xml_count and $test_count always have the same value.
When I do the same, except for that I push a simple object to the array, everything works fine if the number of xml users <= 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!
When having 9940 xml user items, the output is as expected 9940/9940.
BUT when having 9941 xml user items, the output is 9941/19881!
And when having 9942 xml user items, the output is 9942/19882!
Suddenly almost 10000 difference! And the content of the user items doesn’t seem to matter… I’ve copied and moved xml user items with the same result…
EDIT: When more than 9940 items, it suddeny doubles so that 9941 items give (9940 x 2) + 1 = 19881.
No difference when using one single xml user item 12000 times. /EDIT
Any idea what’s going on here?
The following insanity solves the problem for me:
You might want to comment out the
test(50000);line.Does it fix it for you?