将数据列表分解为数组
我有一个以下格式的数据列表:
data\n
data\n
data\n
data\n
data\n
现在我尝试将其分解为一个数组
$array = explode("\n", $dataList);
接下来发生的是有一个没有数据的键,我认为这是因为末尾的 \n 。
有没有办法爆炸它,这样最后一个键就不会被设置?
谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不直接。您可以:
trim
删除尾随的“\n”。array_pop
删除$array
的最后一个元素。preg_split
代替标志PREG_SPLIT_NO_EMPTY
。Not directly. You can either:
trim
.$array
witharray_pop
.preg_split
instead with the flagPREG_SPLIT_NO_EMPTY
.通过以下方式删除空值:
$array = array_filter( $array );
Remove empty values by:
$array = array_filter( $array );
爆炸后,使用 array_pop() 弹出最后一项:
您可以使用 < 添加 if 语句a href="http://www.php.net/count" rel="nofollow noreferrer">count() 和 empty() 如果您想检查最后一项是否包含除换行符之外的其他内容,但这应该可以满足您的需求。
After you explode, use array_pop() to pop the last item:
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.