开发者

Get values from DB field into an array with PHP

I have a field in my DB that holds value separated by commas like;

$tmp_list = "COB,ISJ,NSJ,"

Now when I fetch that the row, I would like to have them in an array. I have used array($tmp_list) but I get the values in one line only like:

[0] => '开发者_开发知识库COB,ISJ,NSJ,'

instead of

[0] => 'COB',
[1] => 'ISJ',
[2] => 'NSJ'

All help is appriciated.


Use explode:

$arr = explode(',', $tmp_list);

If you like, remove the trailing comma using rtrim first:

$arr = explode(',', rtrim($tmp_list, ','));

You can also trim each element if there's a chance of getting any unwanted leading/trailing whitespace in one or more elements (as per @machine's suggestion):

$arr = array_map('trim', explode(',', rtrim($tmp_list, ',')));
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜