将数据列表分解为数组

发布于 2024-09-12 01:24:59 字数 251 浏览 14 评论 0 原文

我有一个以下格式的数据列表:

data\n
data\n
data\n
data\n
data\n

现在我尝试将其分解为一个数组

$array = explode("\n", $dataList);

接下来发生的是有一个没有数据的键,我认为这是因为末尾的 \n 。

有没有办法爆炸它,这样最后一个键就不会被设置?

谢谢!

I've got a list of data with in the following format:

data\n
data\n
data\n
data\n
data\n

Now I try to explode it into an array with

$array = explode("\n", $dataList);

What happens next is that there is a key with no data, I think it is because of the \n on the end.

Is there a way to explode it so that the last key isn't set?

Thanks!

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

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

发布评论

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

评论(3

难如初 2024-09-19 01:24:59

不直接。您可以:

  • 使用 trim 删除尾随的“\n”。
  • 使用 array_pop 删除 $array 的最后一个元素。
  • 使用 preg_split 代替标志 PREG_SPLIT_NO_EMPTY

Not directly. You can either:

  • Remove the trailing "\n" with trim.
  • Remove the last element of $array with array_pop.
  • Use preg_split instead with the flag PREG_SPLIT_NO_EMPTY.
梦屿孤独相伴 2024-09-19 01:24:59

通过以下方式删除空值:

$array = array_filter( $array );

Remove empty values by:

$array = array_filter( $array );

同尘 2024-09-19 01:24:59

爆炸后,使用 array_pop() 弹出最后一项:

$array = explode("\n", $dataList);

array_pop($array);

您可以使用 < 添加 if 语句a href="http://www.php.net/count" rel="nofollow noreferrer">count() 和 empty() 如果您想检查最后一项是否包含除换行符之外的其他内容,但这应该可以满足您的需求。

After you explode, use array_pop() to pop the last item:

$array = explode("\n", $dataList);

array_pop($array);

You can add an if statement using count() and empty() if you want to check if the last item contains something other than a linebreak character, but that should get you what you need.

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