0

I have been struggling with a minor issue with the array_unique for a couple of days now.

Somehow the output always leaves the last duplicate in the array.

I am getting the text from a text box in an html form

$IDs = trim($_POST['IDs']);
$IDs = explode("\n", $IDs);
$IDs = array_filter($IDs, 'trim');
$ID = array_unique($IDs,0);
print_r($ID);

sample input:

012345 0123456 01234567 012345 0123456 01234567 012345 0123456 01234567 sample output:

Array ( [0] => 012345 [1] => 0123456 [2] => 01234567 [3] => 01234567 )

sample input:

012345 0123456 01234567 012345 0123456 01234567 012345 0123456 sample output:

Array ( [0] => 012345 [1] => 0123456 [2] => 01234567 [3] => 0123456 ) 

not sure why the last duplicate keeps getting missed.

Am sure I am missing something but cannot seem to figure it out.

Added the foreach loop hoping to fix it but even with that I keep getting the same result.

rm65453
  • 81
  • 1
  • 8

2 Answers2

2

You should use array_map instead of array_filter.

Like:

$IDs = trim($_POST['IDs']);
$IDs = explode("\n", $IDs);
$IDs = array_map('trim', $IDs);
$ID = array_unique($IDs,0);
print_r($ID);
segFault
  • 3,887
  • 1
  • 19
  • 31
  • This did the trick. Thanks to @kkaosninja as well for pointing out the space thing. Google did bring up the issues with array_filter vs array_map. – rm65453 Sep 24 '16 at 19:13
  • Without knowing the value of `$_POST['IDs']` how did you answered this question? – mertyildiran Sep 24 '16 at 19:21
-1

I have corrected your data format and foreach() is unnecessary:-

<?php

$IDs = "012345\n0123456\n01234567\n012345\n0123456\n01234567\n012345\n0123456\n01234567";

$IDs = explode("\n", $IDs);
$IDs = array_unique($IDs,0);

print_r($IDs);

?>

and the output will be:

Array
(
    [0] => 012345
    [1] => 0123456
    [2] => 01234567
)

P.S. I realized that also array_filter was unnecessary.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
mertyildiran
  • 6,477
  • 5
  • 32
  • 55
  • @Anant I didn't change array structure. Formatting of the input in the question was faulty/wrong. – mertyildiran Sep 24 '16 at 19:14
  • not sure where you made the changes. could you highlight them? – rm65453 Sep 24 '16 at 19:14
  • @mertyildiran IRL you don't always have control over inputs used. – segFault Sep 24 '16 at 19:15
  • @sebastianForsberg Then give me the input in correct formatting please. – mertyildiran Sep 24 '16 at 19:16
  • The input is coming from a text box. might be the correct answer but does not work for me due to my restrictions on the input. – rm65453 Sep 24 '16 at 19:16
  • @rm65453 You gave us the string `012345\n0123456\n01234567\n012345\n0123456\n01234567\n012345\n0123456\n01234567` in your question as the value of `$_POST['IDs']`. So you formatting was wrong on your question. – mertyildiran Sep 24 '16 at 19:19
  • the string was entered in a text box. you are correct with the string that you were providing but that was the whole issue. Somehow it was not behaving the way it should have the filters. – rm65453 Sep 24 '16 at 19:23
  • @rm65453 I have flagged your question because: "This question has severe formatting or content problems. This question is unlikely to be salvageable through editing, and might need to be removed." You were need to give us the output of `echo $_POST['IDs'];` in correct formatting. See http://stackoverflow.com/help/formatting – mertyildiran Sep 24 '16 at 19:26
  • @mertylildiran sure if thats what you want. most of the users were able to figure out what was going on. Anyways my issue has been resolved and hopefully this helps someone else out in the future. – rm65453 Sep 24 '16 at 19:31
  • @rm65453 I couldn't. Please learn how to format your posts properly. – mertyildiran Sep 24 '16 at 19:39