在一个逗号分隔的列表中删除一个值的最快方法 [英] Fastest way of deleting a value in a comma separated list
问题描述
我已经有了用逗号(它们可能含有其他字符),或者是空的,但分隔的名称列表通常看起来像这样:
NAMEA,NameB,NameC
我需要创建一个函数来删除名字,如果在列表中的present和恢复逗号分隔的结构。
例如:如果NAMEA是要被删除,我应该结束
NameB,NameC
不是
,NameB,NameC
同样的休息。
这是我想出了,有没有更好的解决办法?
$ =件爆炸(,$清单); $键= array_search($ deleteuser,$件);
如果(假的!== $键)
{
未设置($件[$关键]);
} $列表=破灭(,,$件);
您可以使用的 array_splice 函数从数组中删除。随着偏移量= array_search($ deleteuser,$件)和长度= 1。
I've got a list of names separated by commas (they may contain other characters), or be empty, but generally looking like this:
NameA,NameB,NameC
I need to create a function to delete a name if its present in the list and which restores the comma separated structure.
eg: if NameA is to be deleted, I should end up with:
NameB,NameC
NOT
,NameB,NameC
Similarly for the rest.
This is what I came up with, is there a better solution?
$pieces = explode(",", $list);
$key=array_search($deleteuser, $pieces);
if(FALSE !== $key)
{
unset($pieces[$key]);
}
$list = implode(",", $pieces);
You could use the array_splice function to delete from the array. With offset = array_search($deleteuser, $pieces) and length = 1.
这篇关于在一个逗号分隔的列表中删除一个值的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!