PHP array_unique function :- A php function which removes duplicate values from an array.

The array_unique() function removes duplicate values from given array. If two or more array values are the same, the first value will be kept and the other will be removed from that array.

Example 1 :-

<?php
$my_array=array("x"=>"Sunday","y"=>"Monday","z"=>"Tuesday","a"=>"Sunday");
echo "<pre>";
print_r(array_unique($my_array));
echo "</pre>";
?>

Output :-

Array
(
    [x] => Sunday
    [y] => Monday
    [z] => Tuesday
)

Example 2 :-

<?php
$input = array(11, "11", "4", 4, 11, "4");
$result = array_unique($input);
echo "<pre>";
var_dump($result);
echo "</pre>";
?>

Output :-
array(2) {
  [0]=>
  int(11)
  [2]=>
  string(1) "4"
}

It will return 11 as an integer value & 4 as a string value.
Advertisements

I like to hear from you about this !!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s