从POST数组中删除重复的数据 [英] Remove Duplicate Data from POST array

查看:136
本文介绍了从POST数组中删除重复的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一个答案,但似乎没有一个实际帮助我的具体情况。我正在尝试发布一个单词列表,然后删除表单中出现的重复数据(单词)。



由于某种原因我似乎无法获得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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆