开发者

Explode datalist into array

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!


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.


Remove empty values by:

$array = array_filter( $array );


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜