如何从PHP中的非唯一字符串中获取唯一的逗号分隔字符串
我有一个 php 字符串
$select_columms = 'pid, p.title, caption, filepath, filename, encaption, user1, user2, user3, user4, user5, user6, ult_puja, p.title, user2, user3';
,所以我有这个想法,我可以用这行代码得到一个唯一的字符串
$select_columns = implode(',', array_unique(array_filter(explode(',',$select_columns))));
,但它似乎不起作用,你能看到我缺少什么吗?
编辑: 感谢的帮助,我的最终代码是:
$select_columns = implode(',', array_filter(array_unique(explode(',', $select_columns))));
输出 $select_columns = 'pid, p.title, caption, filepath, filename, encaption, user1, user2, user3, user4, user5, user6 , ult_puja'
我使用 array_filter 以防我有像 ', pid,' 这样的输入
i have a php string
$select_columms = 'pid, p.title, caption, filepath, filename, encaption, user1, user2, user3, user4, user5, user6, ult_puja, p.title, user2, user3';
so i had this idea i could get a unique string with this line of code
$select_columns = implode(',', array_unique(array_filter(explode(',',$select_columns))));
but it doesn't seem to work can you see what i am missing?
edit:
thanks for the help my final code is:
$select_columns = implode(',', array_filter(array_unique(explode(',', $select_columns))));
which outputs $select_columns = 'pid, p.title, caption, filepath, filename, encaption, user1, user2, user3, user4, user5, user6, ult_puja'
i use the array_filter in case i have input like ', pid,'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
试试这个
编辑正如@amitchd指出的那样。不修剪时很难失败。现已修复
如果你的字符串间距不均匀,你可以这样做
Try this out
EDIT As @amitchd pointed out. Fails hard when not trimmed. Fixed now
And if your string has uneven spacing, you can do this
对我来说似乎很好。 2 个问题:
1) 你在变量声明中拼写了“
columns
”错误2)
array_filter()
在这里是多余的 - 没有它它的工作原理完全相同。演示:
seems fine to me. 2 issues:
1) you've spelt "
columns
" wrong in the variable declaration2)
array_filter()
is redundant here - it works exactly the same without it.demo:
JohnP 的答案应该有效。根据个人喜好,我至少将其分成两行。一行中的三个函数可能很难阅读......
JohnP's answer should work. As a personal preference, I'd separate this out to two lines at least. Three functions on a single line can be hard to read...
Columns 拼写错误,array_filter 不必要,并且爆炸/内爆在逗号后缺少一个空格。
http://ideone.com/clone/b6dZd
Columns was spelt wrong, array_filter was unnecssary and the explode/implode were missing a space after the comma.
http://ideone.com/clone/b6dZd