数组没有返回元素

发布于 2024-10-27 11:11:24 字数 2204 浏览 1 评论 0原文

我问了一个关于相同代码的问题,但在翻译中迷失了,

这就是问题的根源。我验证某个 var 是否是一个数组,然后我想要其中的一个元素但不返回任何值。这是怎么回事?

编辑:我添加了解释字符串来自何处的代码。这是一本留言簿,我需要提出一个不是会话变量数据库的解决方案。这就是我的课程所要求的;

   $file="gb_berichten.txt";            
$datum = date("d-m-Y");

$bericht = "<p><b>naam: </b>$_POST[naam] <br>
            <b>email: </b>$_POST[email] <br>
            <b>onderwerp: </b>$_POST[subj]<br><br>
            <b>bericht: </b>$_POST[bericht]<br>
            <b>verzonden op: </b> $datum
            </p>
            <hr/>
            ";

        if (is_writable ($file)){
            $fp= fopen($file, "a")
                or die ("kan de file niet openen<br>");
                fwrite ($fp, $bericht);
                fclose($fp);
            }//end if writable

        else {
            print ("bestand $file is niet beschikbaar voor schrijven<br>");
            }   

        if (is_readable($file)){
            $fp = fopen($file, "r");
            //$tekst = fread($fp, filesize($file));
            $inhoud = file_get_contents($file);
            $berichten =  implode(' ', array_reverse(explode("<hr/>", $inhoud)));
            fclose ($fp); 
            print($berichten);
            }

        else {
            print("niet mogelijk om bestand te lezen");
            } 




    //check of $berichten een string is
    if (is_string($berichten)){
       print ("string ok"); 
    } else{
       print ("string niet ok");} 
       // returns string ok 

       //convert back to array  
       $berichtenArr =  explode("<hr/>", $berichten);
       if (is_array($berichtenArr )) {
          print("<p style='color:red'>Array OK<p>");
       } else {
          print("<p style='color:red'>Array not OK<p>");
       }
       //returns Array OK

       $secondElem = $berichtenArr[1];
       print("<p style='color:red'>second element is: $secondElem<p>"); // returns no value

       $aantalBer = count($berichtenArr);
       print("<p style='color:red'>amount of messages   is: $aantalBer<p>"); //returns 1
    }

I asked a question about same code but got lost in translation

Here's what the question comes down to. I validate if a certrain var is a an array, then I want an element out of it but returns no value. What's going on?

EDIT: I'm adding the code that explains where the string is coming from. It's a guestbook where I need to come up with a solution that is NOT a database of session variable. It's what my course requires;

   $file="gb_berichten.txt";            
$datum = date("d-m-Y");

$bericht = "<p><b>naam: </b>$_POST[naam] <br>
            <b>email: </b>$_POST[email] <br>
            <b>onderwerp: </b>$_POST[subj]<br><br>
            <b>bericht: </b>$_POST[bericht]<br>
            <b>verzonden op: </b> $datum
            </p>
            <hr/>
            ";

        if (is_writable ($file)){
            $fp= fopen($file, "a")
                or die ("kan de file niet openen<br>");
                fwrite ($fp, $bericht);
                fclose($fp);
            }//end if writable

        else {
            print ("bestand $file is niet beschikbaar voor schrijven<br>");
            }   

        if (is_readable($file)){
            $fp = fopen($file, "r");
            //$tekst = fread($fp, filesize($file));
            $inhoud = file_get_contents($file);
            $berichten =  implode(' ', array_reverse(explode("<hr/>", $inhoud)));
            fclose ($fp); 
            print($berichten);
            }

        else {
            print("niet mogelijk om bestand te lezen");
            } 




    //check of $berichten een string is
    if (is_string($berichten)){
       print ("string ok"); 
    } else{
       print ("string niet ok");} 
       // returns string ok 

       //convert back to array  
       $berichtenArr =  explode("<hr/>", $berichten);
       if (is_array($berichtenArr )) {
          print("<p style='color:red'>Array OK<p>");
       } else {
          print("<p style='color:red'>Array not OK<p>");
       }
       //returns Array OK

       $secondElem = $berichtenArr[1];
       print("<p style='color:red'>second element is: $secondElem<p>"); // returns no value

       $aantalBer = count($berichtenArr);
       print("<p style='color:red'>amount of messages   is: $aantalBer<p>"); //returns 1
    }

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

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

发布评论

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

评论(4

淡水深流 2024-11-03 11:11:24

您已经完成了 explode("


")
,但在其他地方的一条评论中,您声明您的字符串有多个


元素。

你能发现那里的问题吗?

我给你一个线索:



不同。

您可能需要修改 explode() 以将两者考虑在内。

您也可能将


作为另一种有效的可能性。 (事实上​​,还有更多的可能性,因为


标记包含 idclassstyle 属性,所有这些都会被简单的 explode() 错过。

如果您想获取所有有效的可能性,您可能需要使用类似 的属性。 preg_split() 代替。(请注意,尽管我建议这样做,但我应该补充一点,使用正则表达式在 HTML 中进行模式匹配通常被认为是不好的形式。但是因为您只是在寻找简单的


标签,应该可以使用 preg_match() 完成足够好的工作,而不必使匹配模式过于复杂)。

You've done explode("<hr/>"), but in one of your comments elsewhere, you state that your string has multiple <hr /> elements.

Can you spot the problem there?

I'll give you a clue: <hr/> is not the same as <hr />.

You probably need to modify your explode() to take both into account.

It's also possible that you might have <hr> as another valid possibility. (In fact, there's a lot more possiblities than that, since is is valid for the <hr> tag to contain id, class and style attributes, all of which will be missed by a simple explode().

If you want to pick up all valid possibilities, you may want to use something like preg_split() instead. (note although I suggest that, I should add that it is generally considered bad form to use a regex to do pattern-matching in HTML. But since you're just looking for the simple <hr> tag, it should be possible to do a good enough job with preg_match() without having to make your matching pattern too complex).

冰火雁神 2024-11-03 11:11:24

您的数组中只有一个元素。这就是为什么当你尝试获取第二个元素时你没有得到任何值。

添加

print_r($berichtenArr);

,一切都会清楚。

You only have one element in your array. That's why you get no value when you try to get the second element.

Add

print_r($berichtenArr);

and all will be clear.

梅窗月明清似水 2024-11-03 11:11:24

根据您的代码,您的数组仅包含 1 个元素,并且位于索引 0 上。

当您打印数组值时,您引用的是索引 1。

$secondElem = $berichtenArr[0];

并通过打印它来检查数组结构 print_r($berichtenArr)

As per your code your array contains only 1 element so and which is on index 0 .

when you are printing the array value you are referring to index 1.

$secondElem = $berichtenArr[0];

and also check the array structure by printing it print_r($berichtenArr)

半葬歌 2024-11-03 11:11:24

我认为你的逻辑有问题:函数explode()接受一个字符串作为输入,并且在前面的行中你已经确保它不是一个字符串,所以该代码将不起作用。

编辑:抱歉,我错过了第一个结束}(似乎太多了......),但你仍然需要包装你的第一个if 语句围绕所有其余代码,因为如果第一个 if 的结果告诉您它不是字符串,则您无法继续您的 explode

I think there's something wrong with your logic: the function explode() takes a string as input and in the previous lines you have made sure that it is not a string, so that code will not work.

Edit: Sorry, I missed the first closing } (there seems to be one too much...) but you still need to wrap your first if statement around all the rest of your code as you cannot continue with your explode if the result of the first if tells you it´s not a string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文