将数据列表分解为数组 [英] Explode datalist into array

查看:74
本文介绍了将数据列表分解为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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);

接下来发生的事情是,有一个没有数据的密钥,我认为这是因为最后有\ n.

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?

谢谢!

推荐答案

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

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

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

array_pop($array);

您可以使用 count()

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.

这篇关于将数据列表分解为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆