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.

8
  • Why are you doing this: isset($k[$v]) || ($k[$v]=1) && $ID[] = $v;? Commented Sep 24, 2016 at 19:05
  • even without the loop, I was missing the duplicate. Thought if I just loop through it again, the problem with will resolved but it didnt. even if I dont run the loop the issue remains. Commented Sep 24, 2016 at 19:06
  • isset($k[$v]) || ($k[$v]=1).... this will always evaluate to true, since you are setting $k[$v] = 1. Maybe $k[$v] == 1 Commented Sep 24, 2016 at 19:06
  • This is just a guess. But looking at your sample input, both the final values in each array seem to have a trailing space, which if true, will make them unique. The first values in both the inputs seem to have a space preceding them as well. Commented Sep 24, 2016 at 19:06
  • k, loop removed. the issue remains. Commented Sep 24, 2016 at 19:06

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Without knowing the value of $_POST['IDs'] how did you answered this question?
-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.

10 Comments

@Anant I didn't change array structure. Formatting of the input in the question was faulty/wrong.
not sure where you made the changes. could you highlight them?
@mertyildiran IRL you don't always have control over inputs used.
@sebastianForsberg Then give me the input in correct formatting please.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.