0

I want to get only the distinct values from $pjt. I have tried the below code:

$unique_pjtdata = array_unique($pjt);
foreach($unique_pjtdata as $val) {
     echo $val;
}

I am getting an HTTP Error 500 after trying this code.

phwt
  • 1,356
  • 1
  • 22
  • 42
FRECEENA FRANCIS
  • 181
  • 2
  • 13

2 Answers2

2

Use array_unique().

Example:

$pjt = array(1, 2, 2, 3);
$array = array_unique($pjt);

and if you still get error then you need to enable error reporting to know error.

akraf
  • 2,965
  • 20
  • 44
Akash Bhalani
  • 184
  • 1
  • 5
0

You can use array_flip() to switch the keys and values in the array therefore forcing duplicate values to overwrite into a unique set of keys:

$b = ["test1", "test2", "test1", "test3"];
$b = array_flip($b);

print_r( $b );

//output
Array
(
  [test1] => 2
  [test2] => 1
  [test3] => 3
)

You can then extract the keys using array_keys():

$b = array_keys($b);

print_r($b)

// output
Array
(
  [0] => test1
  [1] => test2
  [2] => test3
)
Ivan86
  • 5,695
  • 2
  • 14
  • 30