simpleXml 到数组的行为对我来说看起来很奇怪 - 在 9941 项处中断

发布于 2024-10-07 02:01:12 字数 2663 浏览 5 评论 0原文

EDIT3:问题似乎发生在我的本地主机 XAMPP PHP 5.3 设置上,而不是我测试过的任何运行 php 5.2 的远程服务器上。仍然不清楚是否是 php 或 xampp (或者可能是组合)导致错误 /EDIT3

我有一个包含大约 12000 个名称的 xml 要添加到数组中。 xml结构看起来像这样:

<smdusersync datetime="2010-12-13 13:51:16">
  <userstoadd>
    <User fnamn="Adam" enamn="Svensson" email="[email protected]" password="3906" />
    <User fnamn="Brooke" enamn="Jarnbjer" email="[email protected]" password="2729" />
    <User fnamn="Caesar" enamn="Carlsson" email="[email protected]" password="1668" />
   <!-- about 12000 other users -->
  </userstoadd>
  <userstoremove>
  </userstoremove>
</smdusersync>

EDIT2:我尝试过其他xml示例,包括以编程方式生成的没有属性等,这并不重要 - 仍然是下面描述的相同问题... /EDIT2

当运行一个简单的foreach循环时在 xml userstoadd child 上,奇怪的事情开始发生 当我将对象推入数组时会发生这种情况。

(请注意,下面的示例包含导致错误的代码 - 它无论如何都不是一个有用的示例...)

运行一个简单的 foreach 循环(这里只是将 testcounter 推入数组)按预期工作:

    $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 和$test_count 始终具有相同的值。

当我执行相同操作时,除了将一个简单对象推送到数组之外,如果 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!

当有 9940 个 xml 用户项时,输出按预期 9940/9940 。 但是当有 9941 个 xml 用户项时,输出为 9941/19881! 当有 9942 个 xml 用户项时,输出为 9942/19882!

一下子差了快一万了!用户项目的内容似乎并不重要...我复制并移动了 xml 用户项目,结果相同...

编辑:当超过 9940 个项目时,它突然加倍,以便 9941 个项目给出(9940 x 2) + 1 = 19881。 使用单个 xml 用户项 12000 次时没有区别。 /编辑

知道这里发生了什么吗?

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="[email protected]" password="3906" />
    <User fnamn="Brooke" enamn="Jarnbjer" email="[email protected]" password="2729" />
    <User fnamn="Caesar" enamn="Carlsson" email="[email protected]" 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

べ映画 2024-10-14 02:01:12

以下疯狂的内容为我解决了问题:

您可能需要注释掉 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>';
}
?>

它能为您解决问题吗?

The following insanity solves the problem for me:

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

<?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 it fix it for you?

和我恋爱吧 2024-10-14 02:01:12

正如您所描述的,我看不出与 XML 处理有任何关系。请运行此代码来验证:

$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);

它应该打印数字 9950 三次。如果您的机器上不是这种情况,则您的 PHP 已损坏。

As you describe it I can't see any relation to XML handling. Please run this code to verify:

$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);

It should print the number 9950 three times. If that's not the case on your machine, your PHP is broken.

贱贱哒 2024-10-14 02:01:12

好的,我已经隔离了与我的 XAMPP php 5.3 设置相关的内容。
使用 php 5.2 在两个不同的远程服务器上进行测试,它按预期工作。

以下代码在我的 XAMPP 上使用 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;
}

Ok, I've isolated the as somhow related to my XAMPP php 5.3 setup.
Test on two different remote servers with php 5.2 and there it works as expected.

The following code breaks on my XAMPP with 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文