从POST数组中删除重复的数据 [英] Remove Duplicate Data from POST array
问题描述
由于某种原因我似乎无法获得array_unique工作。 PHP不断给我错误说我的帖子数组是一个字符串。但是如果我尝试使用explode,它说我正在使用一个数组。真的很困惑,非常沮丧。
我的代码很简单:
if(!empty($ _ POST ['keywords']))
{
$ posted = $ _POST ['keywords'];
$ posted = array_unique($ posted);
echo $ posted;
}
我不一定在寻找一个确切的答案,但有一些指导,所以我可以更好地了解我在这里做错什么。
表单:
< form action =<?php $ _SERVER ['PHP_SELF'];?>方法= POST >
< p>
< textarea name =keywordsrows =20columns =120>< / textarea>
< / p>
< p>
< input type =submitname =submit/>
< / p>
< / form>
考虑先用空格拆分关键字参数,然后找到唯一的值:
$ posted = array_unique(explode('',str_replace(\\\
,'',$贴)));
I have been looking for an answer for this, but none seem to actually help my specific situation. I'm trying to post a list of words and then remove the duplicate data (words) that come from the form.
For some reason I can't seem to get array_unique to work. PHP keeps giving me errors saying my post array is a string. But if I try using explode, it says I'm using an array. Really confused right now and very frustrated.
My code is simple:
if(!empty($_POST['keywords']))
{
$posted = $_POST['keywords'];
$posted = array_unique($posted);
echo $posted;
}
I'm not necessarily looking for an exact answer, but some guidance so I can better understand what I'm doing wrong here.
The form:
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<p>
<textarea name="keywords" rows="20" columns="120"></textarea>
</p>
<p>
<input type="submit" name="submit" />
</p>
</form>
Consider first splitting the keywords argument by spaces, then finding the unique values:
$posted = array_unique(explode(' ', str_replace("\n", ' ', $posted)));
这篇关于从POST数组中删除重复的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!