1

I have this input form:

<form method="POST" action="unique_value_processor.php">
<textarea cols="50" rows="8" name="usedurls"></textarea>
<textarea cols="50" rows="8" name="freshurls"></textarea>
<textarea cols="50" rows="8" name="filteredurls"></textarea>
<input type="SUBMIT" value="SUBMIT">

The files that processes the form is

$old_urls_exploded = explode("\n", $_POST['usedurls']);
$new_urls_exploded = explode("\n", $_POST['freshurls']);
$arraydiff = array_diff($new_urls_exploded, $old_urls_exploded);
print_r($arraydiff);

So when I input the following into the form:

box 1 (old_urls_exploded):

blue, yellow

box 2 (new_urls_exploded):

yellow, blue, banana

then it SHOULD return only:

banana

But array_diff returns:

yellow, banana

Then when you manually type the array as:

$old_urls_exploded = array('blue','yellow');
$new_urls_exploded = array('yellow','blue','banana');

then array_diff returns only:

banana

as it should..

Why does the form affect how array_diff behaves? Am I doing something wrong?

Robert Sinclair
  • 4,550
  • 2
  • 44
  • 46
  • 4
    Does this do the trick: `$arraydiff = array_diff(array_map("trim", $new_urls_exploded), array_map("trim", $old_urls_exploded));` ? – Rizier123 Jul 09 '15 at 14:13
  • In your second example, how does it return banana when banana is in neither array? ;) Rizier123 is trimming the inputs. That means he thinks you might have some whitespace on the end of each line in your textinputs making them comparatively different. – crush Jul 09 '15 at 15:07
  • Rizier123 Yes, that solved it, thank you very much! – Robert Sinclair Jul 09 '15 at 20:05

1 Answers1

2

As mentioned by @Rizier123 the problem was white spaces added when the form was input.

So it was solved using:

$arraydiff = array_diff(array_map("trim", $new_urls_exploded), array_map("trim", $old_urls_exploded));

Thank you

Robert Sinclair
  • 4,550
  • 2
  • 44
  • 46