1

I tried many ways, I have an array:

array(1) { [0]=> string(113) "23138,19031,22951,22951,22962,18858,18858,18858,18858,18858,18858,18858,18858,18858,18858,18858,18858,18858,18858" }

Tried:

$a = array_map("unserialize", array_unique(array_map("serialize", $a)));
var_dump($a);

Or

$a = array_unique($a);
var_dump($a);

And

$a = array_values(array_unique($a));
var_dump($a);

Nothing, I still get duplicated values, the full code would be:

$user_id = get_current_user_id();
$postid = $post->ID;
$userPosts= get_user_meta( $user_id, 'save_post', TRUE );
$userPosts = str_replace(' ', '', $userPosts);
$a = explode(', ', $userPosts);
$a = array_values(array_unique($a));
var_dump($a);
update_user_meta( $user_id, 'save_post', $a );
8
  • Why down votes? Commented Jun 11, 2018 at 7:43
  • 2
    Think in steps. First, you need it as array, split on the comma: Explode. And not you want unique values: array_unique() -> $array = array_unique( explode("," $string) ); Commented Jun 11, 2018 at 7:44
  • You have an array containing one item, which is a string. Not an array with multiple items. Commented Jun 11, 2018 at 7:45
  • I didn't downvote, but maybe because this sounds like it has been answered a lot. Also, with some effort you could have found array_unique() on PHPs manual Commented Jun 11, 2018 at 7:45
  • 1
    $userPosts = str_replace(' ', '', $userPosts); $a = explode(', ', $userPosts); you remove spaces then you try to explode with comma space but there are no spaces, you just removed them, so the output should be one item. One long string that you try to remove duplicates from. This should be cought if you had done proper debugging Commented Jun 11, 2018 at 7:50

3 Answers 3

3

You should use explode() to build an array from your string first:

$x = "23138,19031,22951,22951,22962,18858,18858,18858,18858,18858,18858,18858,18858,18858,18858,18858,18858,18858,18858";
print_r(array_unique(explode(',', $x)));

gives

Array
(
    [0] => 23138
    [1] => 19031
    [2] => 22951
    [4] => 22962
    [5] => 18858
)
Sign up to request clarification or add additional context in comments.

Comments

0
$userPosts = str_replace(' ', '', $userPosts);
$a = explode(', ', $userPosts);

You remove spaces then you try to explode with comma space but there are no spaces, you just removed them, so the output should be one item.
One long string that you try to remove duplicates from which does not work.

Two possible solutions remove the removal of spaces.
Or remove the space from explode.

I would advice to remove the removal of spaces as it's probably not needed, thus just using CPU for nothing.

$user_id = get_current_user_id();
$postid = $post->ID;
$userPosts= get_user_meta($user_id, 'save_post', TRUE );
$a = explode(', ', $userPosts);
$a = array_values(array_unique($a));
var_dump($a);
update_user_meta( $user_id, 'save_post', $a );

EDIT from comments it seems there are no spaces in $userPosts so the working solution is to remove the space from the explode.

$user_id = get_current_user_id();
$postid = $post->ID;
$userPosts= get_user_meta($user_id, 'save_post', TRUE );
$a = explode(',', $userPosts);
$a = array_values(array_unique($a));
var_dump($a);
update_user_meta( $user_id, 'save_post', $a );

4 Comments

i still get duplicated values tho, when i do var_dump($a); i get: array(1) { [0]=> string(17) "20504,20486,20486" }
$userPosts Does it include spaces or not. Your code indicates yes, but now your comment says "probably not". If there is no spaces, make $a = explode(', ', $userPosts); to: $a = explode(',', $userPosts);
ok works with no spaces, thanks. Accepting, yet update the answer with no space pls
Not you but your var_dump whisperd gently in my ears "probably not". Didn't you hear it? Thanks!
0

Your array contains only one value: the string "23138,19031,22951,22951,22962,18858,18858,18858,18858,18858,18858,18858,18858,18858,18858,18858,18858,18858,18858".

You have to split this string into pieces using the explode() function then pass its result to array_unique() to remove the duplicates:

$input = array("23138,19031,22951,22951,22962,18858,18858,18858,18858,18858,18858,18858,18858,18858,18858,18858,18858,18858,18858");

$pieces = explode(',', $input[0]);
$noDups = array_unique($pieces);

// Check the outcome
print_r($noDups);

The output is:

Array
(
    [0] => 23138
    [1] => 19031
    [2] => 22951
    [4] => 22962
    [5] => 18858
)

If you need the values back in a string (separated by comma) you can use implode().

Comments

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.