如何从PHP中的非唯一字符串中获取唯一的逗号分隔字符串

发布于 2024-10-18 22:24:14 字数 750 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

云仙小弟 2024-10-25 22:24:14

试试这个

echo implode(',', array_unique(explode(',', $select_columms)));

编辑正如@amitchd指出的那样。不修剪时很难失败。现已修复

如果你的字符串间距不均匀,你可以这样做

$select_columms = 'pid, p.title, caption, filepath, filename, encaption, user1, user2, user3, user4, user5, user6, ult_puja, p.title, user2, user3';
$arr    = explode(',', $select_columms);
array_walk($arr, '_trim');
echo implode(',', array_unique($arr));

function _trim(&$value) {
    $value = trim($value);   
}

Try this out

echo implode(',', array_unique(explode(',', $select_columms)));

EDIT As @amitchd pointed out. Fails hard when not trimmed. Fixed now

And if your string has uneven spacing, you can do this

$select_columms = 'pid, p.title, caption, filepath, filename, encaption, user1, user2, user3, user4, user5, user6, ult_puja, p.title, user2, user3';
$arr    = explode(',', $select_columms);
array_walk($arr, '_trim');
echo implode(',', array_unique($arr));

function _trim(&$value) {
    $value = trim($value);   
}
初见终念 2024-10-25 22:24:14

对我来说似乎很好。 2 个问题:

1) 你在变量声明中拼写了“columns”错误

2) array_filter() 在这里是多余的 - 没有它它的工作原理完全相同。

演示:

$ php -a
Interactive shell

php > $select_columns = 'pid, p.title, caption, filepath, filename, encaption, user1, user2, user3, user4, user5, user6, ult_puja, p.title, user2, user3';

php > echo  implode(',', array_unique(explode(',',$select_columns)));
pid, p.title, caption, filepath, filename, encaption, user1, user2, user3, user4, user5, user6, ult_puja

php > 

seems fine to me. 2 issues:

1) you've spelt "columns" wrong in the variable declaration

2) array_filter() is redundant here - it works exactly the same without it.

demo:

$ php -a
Interactive shell

php > $select_columns = 'pid, p.title, caption, filepath, filename, encaption, user1, user2, user3, user4, user5, user6, ult_puja, p.title, user2, user3';

php > echo  implode(',', array_unique(explode(',',$select_columns)));
pid, p.title, caption, filepath, filename, encaption, user1, user2, user3, user4, user5, user6, ult_puja

php > 
凯凯我们等你回来 2024-10-25 22:24:14

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...

山色无中 2024-10-25 22:24:14

Columns 拼写错误,array_filter 不必要,并且爆炸/内爆在逗号后缺少一个空格。

<?php
$select_columns = 'pid, p.title, caption, filepath, filename, encaption, user1, user2, user3, user4, user5, user6, ult_puja, p.title, user2, user3';

$select_columns = implode(', ',array_unique(explode(', ',$select_columns)));

var_dump($select_columns);
?>

http://ideone.com/clone/b6dZd

Columns was spelt wrong, array_filter was unnecssary and the explode/implode were missing a space after the comma.

<?php
$select_columns = 'pid, p.title, caption, filepath, filename, encaption, user1, user2, user3, user4, user5, user6, ult_puja, p.title, user2, user3';

$select_columns = implode(', ',array_unique(explode(', ',$select_columns)));

var_dump($select_columns);
?>

http://ideone.com/clone/b6dZd

疯了 2024-10-25 22:24:14
$str = implode(',',array_unique(explode(',', $str)));
$str = implode(',',array_unique(explode(',', $str)));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文