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

John
  • 1
  • 13
  • 98
  • 177
gkopowski
  • 107
  • 1
  • 10
  • 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);` – u_mulder Oct 31 '15 at 14:40

2 Answers2

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

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
3
$input = array(49,49,49);

$result = array_unique($input);

print_r($result);
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Virag Shah
  • 64
  • 2