0

I have an array $tmp:

$a = array(0 => 49, 1 => 49, 2 => 49);

after using array_unique($tmp) I'm getting this output:

Array
(
    [0] => 49
    [1] => 49
    [2] => 49
)

and I want to get

Array
(
    [0] => 49
)

What am I doing wrong? Im new in PHP

1
  • 1
    Obviously you show us not real code, or values in array are somehow different. Maybe with spaces or some hidden symbols around. Do a var_dump($tmp); Commented Oct 31, 2015 at 14:40

2 Answers 2

8

You don't only need to call that function you need to use the returned value as well. Do

$tmp=array_unique($tmp);

Just calling that function and not picking up the returned value does no good.

There are some functions that operate on the original variable and hence you dont need to pick up their ret val for example sort() but array_unique() is not one of them. Always refer to http://www.php.net/functionName to find out

Sign up to request clarification or add additional context in comments.

Comments

3
$input = array(49,49,49);

$result = array_unique($input);

print_r($result);

1 Comment

Drop-in answer; care to "explain"? for future readers to the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.