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:
现在,当我获取该行时,我想将它们放在一个数组中。我使用了数组($ tmp_list),但我只在一行中获取值:
[0] => 'COB,ISJ,NSJ,'
instead of
[0] => 'COB',
[1] => 'ISJ',
[2] => 'NSJ'
All help is appriciated.
所有帮助都是适用的。
1 个解决方案
#1
4
Use explode
:
$arr = explode(',', $tmp_list);
If you like, remove the trailing comma using rtrim
first:
如果您愿意,请先使用rtrim删除尾随逗号:
$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, ',')));
#1
4
Use explode
:
$arr = explode(',', $tmp_list);
If you like, remove the trailing comma using rtrim
first:
如果您愿意,请先使用rtrim删除尾随逗号:
$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, ',')));