Share on Google+Share on Google+

PHP Remove Duplicate Values

Advertisement
PHP Remove Duplicate Values: In this tutorial you will come to know about how to remove the duplicate values, PHP provides array_unique() function, in this tutorial you will come to know about its general format, parameters, return value. The example will exemplify the function of this function

PHP Array Remove Duplicate Values

In PHP if we need to remove duplicate values then we can use array_unique() function. The functionality of it is to accept an array and returns another array without duplicate values.

General description of array_unique() is given below:

General Format array array_unique(array $array [, int $sort_flags=sort_string] )
Parameters array: Input array sort_flags: The second optional parameter is used to compare items as follows
  • SORT_REGULAR - Normal
  • SORT_NUMERIC - Numerically
  • SORT_STRING - As string
  • SORT_LOCALE_STRING - As string, based on the current locale.
Return Value Another array without duplicate values

  Example 1:

<?php

$array=array('cricket'=>11,'football'=>11,'chess'=>2);

echo"<br/><b>Initially the values of \$array is:</b><br/>";

var_dump($array);

echo"<br/><b>After removing the duplicates:</b><br/>";

print_r(array_unique($array));

?>

Output:

Initially the values of $array is:
array(3) {
  ["cricket"]=>
  int(11)
  ["football"]=>
  int(11)
  ["chess"]=>
  int(2)
}

After removing the duplicates:
Array
(
    [cricket] => 11
    [chess] => 2
)

Example 2:

<?php

$array=array("a"=>"adenine","axerophthol","angstrom","b"=>"bacillus","angstrom","barn");

echo"<br/><b>Initially the values of \$array is:</b><br/>";

var_dump($array);

echo"<br/><b>After removing the duplicates:</b><br/>";

print_r(array_unique($array));

?>

Output:

Initially the values of $array is:
array(6) {
  ["a"]=>
  string(7) "adenine"
  [0]=>
  string(11) "axerophthol"
  [1]=>
  string(8) "angstrom"
  ["b"]=>
  string(8) "bacillus"
  [2]=>
  string(8) "angstrom"
  [3]=>
  string(4) "barn"
}

After removing the duplicates:
Array
(
    [a] => adenine
    [0] => axerophthol
    [1] => angstrom
    [b] => bacillus
    [3] => barn
)

Example 3:

<?php

$array=array("1",1,"1","2","2","2",2,2,2,2,3,3,3);

echo"<br/><b>Initially the values of \$array is:</b><br/>";

var_dump($array);

echo"<br/><b>After removing the duplicates:</b><br/>";

print_r(array_unique($array));

?>

Output:

Initially the values of $array is:
array(13) {
  [0]=>
  string(1) "1"
  [1]=>
  int(1)
  [2]=>
  string(1) "1"
  [3]=>
  string(1) "2"
  [4]=>
  string(1) "2"
  [5]=>
  string(1) "2"
  [6]=>
  int(2)
  [7]=>
  int(2)
  [8]=>
  int(2)
  [9]=>
  int(2)
  [10]=>
  int(3)
  [11]=>
  int(3)
  [12]=>
  int(3)
}

After removing the duplicates:
Array
(
    [0] => 1
    [3] => 2
    [10] => 3
)

Advertisements

Advertisement

Posted on: October 26, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.