0

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>
hakre
  • 193,403
  • 52
  • 435
  • 836
714sooner
  • 29
  • 2
  • 5

4 Answers4

0

Consider first splitting the keywords argument by spaces, then finding the unique values:

$posted = array_unique(explode(' ', str_replace("\n", ' ', $posted)));
Nathan
  • 1,700
  • 12
  • 15
  • ...also, when echo'ing the result the variable $posted is an array, so to review its value one would 'echo print_r($posted, true)' (or perhaps simply 'print_r($posted)' – Nathan Dec 15 '11 at 02:12
  • Hey Nathan, I tried that and I get the following: Array ( [0] => test test ) ... it's like it's still calling it as a string. – 714sooner Dec 15 '11 at 02:16
  • i imagine the textarea value contains a new line -- try this revision which first converts newline to space. – Nathan Dec 15 '11 at 02:23
  • Nice, thanks! I added in a return, too. The newline definitely made a change, but it still wasn't taking out the duplicate. Now it works as: `code` $posted = array_unique(explode(' ', str_replace("\r\n", ' ', $posted))); – 714sooner Dec 15 '11 at 02:31
0

Maybe you should look into using array_filter

That way you can define your own callback function for maximum rigorousness on your your removes...

http://php.net/manual/en/function.array-filter.php

Also: Have you tried messing around with the array_unique flags?

http://php.net/manual/en/function.array-unique.php

Zigu
  • 1,625
  • 4
  • 23
  • 33
  • I haven't tried any flags, as I was just building it up slowly. I just can't seem to get any duplicates removed at all at this point. I'll look into flags. Thanks! – 714sooner Dec 15 '11 at 02:23
0

Your keywords form field is setup as a textarea, so when you post, you are posting a string. Try this:

$posted = $_POST['keywords'];

$postedKeywords = explode(' ', $posted);

$posted = array_unique($postedKeywords);
Mike Purcell
  • 19,847
  • 10
  • 52
  • 89
0

The previous answers are great, but since the words are being input to a textfield, the delimiter will be unpredictable. Consider using a regular expression instead:

preg_match_all('/([^\s]+)/', $_POST['keywords'], $matches);
$unique_words = array_unique($matches[0]);
Ayman Safadi
  • 11,502
  • 1
  • 27
  • 41
  • Ayman, I just tried this and it works, too. Thanks for the idea! – 714sooner Dec 15 '11 at 02:35
  • I just realized that there's an issue with this. Since it's removing spaces, it also turns any words with spaces (ie, ayman safadi) into multiple words. – 714sooner Dec 15 '11 at 03:11
  • The same would've happened with: explode(' ', $_POST['keywords']) You can instruct the user to either use commas or line breaks. Or come up with some fancy front-end solution to make sure the user inputs the words correctly. – Ayman Safadi Dec 15 '11 at 03:12
  • Yeah, I can see that now. Both work great for single words with no spaces, but turn words with spaces into separate words. – 714sooner Dec 15 '11 at 03:15
  • @714sooner - Just wanted to make sure you read my edited comment. – Ayman Safadi Dec 15 '11 at 03:17
  • Thanks for that. I will see if there's a way to automatically add commas to the end of each word. Not sure why this is becoming a pain, too, but still trying! ha – 714sooner Dec 15 '11 at 03:32