PHP:explode() 中未定义的偏移量

发布于 2024-11-23 17:12:37 字数 409 浏览 0 评论 0原文

我有这个:

list($firstname, $lastname) = explode(' ', $queryString);

有时 $lastname 没有被定义,并且我在那里遇到未定义的偏移错误。

我猜是因为它找不到任何可以放入 $lastname 的内容。

在explode()之后我有:

if(!$lastname) { $lastname = $firstname; }

所以我的问题是如果未定义$lastname,我如何将它定义为$firstname(如果你只写了'Adam'而不是'Adam Thompson',那么应该定义姓氏,所以它是'Adam Adam')

现在它为我做到了这一点,但我收到了偏移错误

I have this:

list($firstname, $lastname) = explode(' ', $queryString);

Sometiems $lastname does not gets defined, and it's there i am getting undefined offset error.

Because it can not find anything to put in $lastname, i guess.

After the explode() i have:

if(!$lastname) { $lastname = $firstname; }

So my question is how can i define it as the $firstname if $lastname is not defined (if you wrote only 'Adam' and not 'Adam Thompson', the lastname should be defined so it is 'Adam Adam')

It does this for me now, but I am receiving the offset error

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

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

发布评论

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

评论(5

虐人心 2024-11-30 17:12:37
list($firstname, $lastname) = array_pad(explode(' ', $queryString, 2), 2, null);

explode() 中的 2 确保最多有 2 个值,而 array_pad() 确保至少 2 个值。如果没有空格字符 ,则 $lastnamenull。您可以用它来决定下一步要做什么

$lastname = is_null($lastname) ? $firstname : $lastname;

。 小更新:对于这种特定情况,您可以使用一个小技巧,

list($firstname, $lastname) = array_pad(explode(' ', $queryString, 2), 2, $queryString);

这将一步完成所有操作。它应该可以工作,因为

  • 总是至少有一个值(对于 $firstname
  • 如果有一个值,则 $queryString == $firstname。这就是现在用于填充数组最多 2 个值的值(这正好是一个,因为我们已经有一个值)
  • 如果有两个值,则数组不会用 $queryString 填充,因为我们已经有 2 个值

至少为了可读性我更喜欢第一个更明显的解决方案。

list($firstname, $lastname) = array_pad(explode(' ', $queryString, 2), 2, null);

The 2 in explode() ensures, that there are at most 2 values and array_pad() ensures, that there are at least 2 values. If there is no space character , $lastname is null. This you can use to decide what comes next

$lastname = is_null($lastname) ? $firstname : $lastname;

Little update: For this specific case you can use a little trick

list($firstname, $lastname) = array_pad(explode(' ', $queryString, 2), 2, $queryString);

This will do all that in one step. It should work, because

  • There is always at least one value (for $firstname)
  • If there is one value, then $queryString == $firstname. Thats now the value that is used to fill the array up to 2 values (which is exactly one, because one value we already have)
  • If there are two values, then the array is not filled with $queryString, because we already have 2 values

At least for readability I would prefer the first more obvious solution.

不顾 2024-11-30 17:12:37

尝试附加一个空格:

list($firstname, $lastname) = explode(' ', $queryString . ' ' );

之后不必更改任何内容。

Try appending a space:

list($firstname, $lastname) = explode(' ', $queryString . ' ' );

shouldn't have to change a thing after that.

淡水深流 2024-11-30 17:12:37

您收到的不是错误,而是通知

虽然这是可以接受的,因为 PHP 是一种动态语言,但您可以使用 isset( )

if(!isset($lastname)) {
  $lastname = $firstname;
}

更新

根据评论,list()通知的罪魁祸首。在这种情况下,当 explode() 未产生适当数量的参数时,我不建议使用 list()

如果必须,请通过 brady使用时未定义的偏移量来回答phpexplode()可以工作。尽管在我看来它相当。我相信如果您执行以下操作,您的代码会更加直观:

$name = explode(' ', $queryString);

if (isset($name[1])) {
  // show lastname
}
else {
  // show firstname
}

You're not getting an Error, but a Notice.

Although this is acceptable since PHP is a dynamic language, you can prevent it with isset():

if(!isset($lastname)) {
  $lastname = $firstname;
}

UPDATE

Per the comments, list() is the culprit for the Notice. In which case, I wouldn't recommend the use of list() when explode() doesn't yield the appropriate number of parameters.

If you must, the answer by brady or undefined offset when using php explode() can work. Although it's pretty ugly in my opinion. I believe your code would be much more intuitive if you just did the following:

$name = explode(' ', $queryString);

if (isset($name[1])) {
  // show lastname
}
else {
  // show firstname
}
情深已缘浅 2024-11-30 17:12:37

我今天刚遇到这个。我的解决方案不是上面的,
(没有效果)我的如下:

while (!feof($fh))
{
    $line = fgets($fh);
    print $line;
}

而不是这样做:

while ($line = fgets($fh))
{
     print $line;
}

I just ran into this today. my solution was not the above,
(which had no effect) mine was the following:

while (!feof($fh))
{
    $line = fgets($fh);
    print $line;
}

instead of doing:

while ($line = fgets($fh))
{
     print $line;
}
狂之美人 2024-11-30 17:12:37

我不清楚为什么会这样,但通知会消失。首先,使用这段代码,我得到了未定义的偏移通知:

list($month, $day, $year)=explode('-', $dateToChange, 3);

但是,使用这段代码,我没有:

list($month, $day, $year, $junk)=explode('-', $dateToChange.'---', 4);

另请注意,在 $dateToChange 后附加“-”或“--”,我将得到偏移通知。在我的带有四个变量的示例中,需要三个破折号才能消失。 $junk 包含两个破折号(一个是分隔符)。

I'm not clear why this works, but the notice will go away. First, with this code I get the undefined offset notice:

list($month, $day, $year)=explode('-', $dateToChange, 3);

However, with this code, I don't:

list($month, $day, $year, $junk)=explode('-', $dateToChange.'---', 4);

Also note, with '-' or '--' appended to $dateToChange, I will get the offset notice. It takes three dashes for it to go away in my example with four variables. $junk contains the two dashes (one being a separator).

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