【问题标题】:Why is it only showing 2 out of the 3 arrays?为什么它只显示 3 个数组中的 2 个?
【发布时间】:2011-10-13 00:33:32
【问题描述】:

我不确定为什么会这样。这是我的 XML:

<ResultSet>
<chats>
    <chat>
        <messages>
            <sms>
                <from>Bisma Iqbal</from>
                <msg>Hi</msg>
                <sent>1311612055</sent>
            </sms>
            <sms>
                <from>Bisma Iqbal</from>
                <msg>Hi</msg>
                <sent>1311612068</sent>
            </sms>
            <sms>
                <from>Bisma Iqbal</from>
                <msg>Hi</msg>
                <sent>1311612074</sent>
            </sms>
            <sms>
                <from>Bisma Iqbal</from>
                <msg>Hi</msg>
                <sent>1311612186</sent>
            </sms>
        </messages>
        <contact>Bisma Iqbal</contact>
    </chat>
    <chat>
        <messages>
            <sms>
                <from>Javaid Iqbal</from>
                <msg>this is the first message</msg>
                <sent>1311612055</sent>
            </sms>
            <sms>
                <from>Javaid Iqbal</from>
                <msg>this is the second message</msg>
                <sent>1311612055</sent>
            </sms>
        </messages>
        <contact>Javaid Iqbal</contact>
    </chat>
    <chat>
        <messages>
            <sms>
                <from>Ankur Shahi</from>
                <msg>Wanna play dawg at 7:15</msg>
                <sent>1311632708</sent>
            </sms>
            <sms>
                <from>Ankur Shahi</from>
                <msg>Wanna play dawg at 7:15</msg>
                <sent>1311632708</sent>
            </sms>
            <sms>
                <from>Ankur Shahi</from>
                <msg>Wanna play dawg at 7:15</msg>
                <sent>1311632708</sent>
            </sms>
        </messages>
        <contact>Ankur Shahi</contact>
    </chat>
</chats>
</ResultSet>

现在,这是我的代码:

require_once('global.php');
$statement = $db->prepare("SELECT * FROM user WHERE id=1");
$statement->execute();
$result = $statement->fetchObject();
$string = $result->chats;
$chats = GBA($string, "<chat>", "</chat>"); //the XML I showed you

for($i = 0, $size = count($chats); $i < $size; ++$i) {
//for every chat:
    $all_sms = GBA($chats[$i], "<sms>", "</sms>");
    $name = GB($chats[$i], "<contact>", "</contact>");
    echo "<center><table border='1' cellpadding='5'><tr><td colspan='3' align='center'>SMS Chat with <i>{$name}</i></td></tr><tr><th>From</th><th>Message</th><th>Sent</th></tr>";

    for($j = 0, $size = count($all_sms); $j < $size; ++$j) {
    //for every sms in each chat
            $from = GB($all_sms[$j], "<from>", "</from>");
            $msg = GB($all_sms[$j], "<msg>", "</msg>");
            $sent = time_since(GB($all_sms[$j], "<sent>", "</sent>"));
            echo "<tr><td>{$from}</td><td>{$msg}</td><td>Sent: {$sent}   </td></tr>";
        }
    echo "</table><br /><br /></center>";
}

如您所见,一切看起来都很好,但它只显示前 2 个表,而不是第三个!我调试了很多都没有结论。

【问题讨论】:

  • GB 是一个获取其他两个字符串之间的字符串的函数。 GBA 是一种获取其他两个字符串之间的字符串的函数,但会搜索整个字符串并返回一个数组。 Getbetween 和 GetbetweenAll

标签: php xml arrays for-loop


【解决方案1】:

您在外循环中使用了$size,在内循环中使用了$size

PHP 没有块作用域,所以这是同一个变量。当它在内循环中发生变化时,你正在搞乱你的外循环。

为内部循环中的变量选择一个不同的名称。

【讨论】:

    【解决方案2】:

    在内部循环中重新分配 $size。第二个短信聊天组只有 2 个条目,这导致外部循环终止,因为 $size = 2 和 $i = 2。Jon Martin 的建议似乎适用于您的特定数据,但在一般情况下会失败。

    我建议

    for($j = 0, $jsize = count($all_sms); $j < $jsize; ++$j) {
    // ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-20
      • 2021-05-07
      • 1970-01-01
      • 2019-09-15
      • 2021-06-06
      相关资源
      最近更新 更多