PHP 错误:未定义的偏移量:1

发布于 2024-10-25 08:51:59 字数 722 浏览 3 评论 0原文

看起来这可能是处理数组时的错误,但我无法弄清楚。我真的刚刚开始接触 PHP,这有点令人生畏。 任何帮助将不胜感激!这是我的代码:

<?php echo "<h1>Choose a Poll!</h1>";

$read = file('poll_topics.txt');   
$data = array( );           
foreach($read as $lines){           
    list($key,$v) = explode("|","$lines");          
    $data[$key] = $v;
}

foreach ($data as $k=>$desc){               
    echo "<ul><li><a href='take_a_poll.php?poll=$k'>$k</a> - $desc </li></ul>";
}

?>

这是文本文件中的内容:

  Instruments|What kind of instruments do you like?
  Music|What type of music do you like best?

我应该澄清: 错误出现在第 20 行,或者显示 list($key,$v) =explode... 的位置

It seems like this might be an error dealing with arrays, but I can't figure it out. I'm really just starting with PHP and this is getting to be a little intimidating.
Any help would be greatly appreciated! here is my code:

<?php echo "<h1>Choose a Poll!</h1>";

$read = file('poll_topics.txt');   
$data = array( );           
foreach($read as $lines){           
    list($key,$v) = explode("|","$lines");          
    $data[$key] = $v;
}

foreach ($data as $k=>$desc){               
    echo "<ul><li><a href='take_a_poll.php?poll=$k'>$k</a> - $desc </li></ul>";
}

?>

Here is what is in the text file:

  Instruments|What kind of instruments do you like?
  Music|What type of music do you like best?

I should clarify:
The error is line 20, or where it says list($key,$v) = explode...

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

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

发布评论

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

评论(3

哑剧 2024-11-01 08:51:59

你在某处有一个空行。这就是为什么 explode() 将仅返回一个空的 $key,但没有任何内容可分配给 $v。这就是它打印该通知的时间。

您可以稍微重写它以忽略此类情况:

foreach ($read as $lines) {
    $key = strtok($lines, "|");
    $v = strtok("|");
    if ($v) {
        $data[$key] = $v;
    }
}

这也将避免最终 $data 数组中出现空条目。

You have an empty line somewhere. That's why explode() will return only an empty $key, but have nothing to assign to the $v. And that's when it prints that notice.

You can rewrite it a bit to ignore such cases:

foreach ($read as $lines) {
    $key = strtok($lines, "|");
    $v = strtok("|");
    if ($v) {
        $data[$key] = $v;
    }
}

This will also avoid an empty entry in your final $data array.

对你的占有欲 2024-11-01 08:51:59

试试这个:

<?php 

echo "<h1>Choose a Poll!</h1>";
$_fileData = file_get_contents('poll_topics.txt'); 
$_results = array();

if ( ! empty( $_fileData ) )
{
    foreach ( $_fileData as $_line ) 
    {
        $_split = explode( '|', $_line );

        //  Many ways to do this:
        //  if ( !empty( $_split ) && 2 == count( $_split ) ) then no error else error
        //  or...
        if ( isset( $_split[0], $_split[1]  ) )
        {
            $_key = $_split[0];
            $_value = $_split[1];

            if ( null !== $_key && null !== $_value )
            {
                $_results[ $_key ] = $_value;
                    //  or  $_results[] = array( $_key => $_value ); if key can be duplicated
            }

        }
    }
}

Try this:

<?php 

echo "<h1>Choose a Poll!</h1>";
$_fileData = file_get_contents('poll_topics.txt'); 
$_results = array();

if ( ! empty( $_fileData ) )
{
    foreach ( $_fileData as $_line ) 
    {
        $_split = explode( '|', $_line );

        //  Many ways to do this:
        //  if ( !empty( $_split ) && 2 == count( $_split ) ) then no error else error
        //  or...
        if ( isset( $_split[0], $_split[1]  ) )
        {
            $_key = $_split[0];
            $_value = $_split[1];

            if ( null !== $_key && null !== $_value )
            {
                $_results[ $_key ] = $_value;
                    //  or  $_results[] = array( $_key => $_value ); if key can be duplicated
            }

        }
    }
}
趁年轻赶紧闹 2024-11-01 08:51:59

您可以尝试使用array_pad()函数。
在编写爆炸函数的地方使用它。

$_split = array_pad(explode( '|', $_line ), numberOfElementsInArray, null);

You could try to use the array_pad() function.
Use it where you wrote the explode function.

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