Home Tutorial Php Phpbasics PHP Array Unique

 
 

Share on Google+Share on Google+
PHP Array Unique
Posted on: October 30, 2009 at 12:00 AM
Advertisement
PHP Array Unique:In this tutorial you will get to know about the array_unique() function. which eliminates the duplicate values. This tutorial has several examples, in which few of them are similar but has very minor but important differences.

PHP Array Unique

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

 $array1=array("a"=>"Apple","b"=>"Banana","o"=>"Orange","b"=>"Apple","b"=>"Apple");

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

var_dump($array1);

echo"<br/><b>New array without duplicate values is:</b><br/>";

print_r(array_unique($array1));

?>

Output:

Initially the values of $array1 is:
array(3) {
  ["a"]=>
  string(5) "Apple"
  ["b"]=>
  string(5) "Apple"
  ["o"]=>
  string(6) "Orange"
}

New array without duplicate values is:
Array
(
    [a] => Apple
    [o] => Orange
)

Example 2:

<?php

$array1=array("Apple","Banana","Orange","Apple","Apple");

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

var_dump($array1);

echo"<br/><b>New array without duplicate values is:</b><br/>";

print_r(array_unique($array1));

?>

Output:

Initially the values of $array1 is:
array(5) {
  [0]=>
  string(5) "Apple"
  [1]=>
  string(6) "Banana"
  [2]=>
  string(6) "Orange"
  [3]=>
  string(5) "Apple"
  [4]=>
  string(5) "Apple"
}

New array without duplicate values is:
Array
(
    [0] => Apple
    [1] => Banana
    [2] => Orange
)

Example 3:

<?php

$array1=array("a"=>"Apple","b"=>"Banana","o"=>"Orange","a"=>"Apple","a"=>"Apple");

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

var_dump($array1);

echo"<br/><b>New array without duplicate values is:</b><br/>";

print_r(array_unique($array1));

?>

Output:

Initially the values of $array1 is:
array(3) {
  ["a"]=>
  string(5) "Apple"
  ["b"]=>
  string(6) "Banana"
  ["o"]=>
  string(6) "Orange"
}

New array without duplicate values is:
Array
(
    [a] => Apple
    [b] => Banana
    [o] => Orange
)


Example 4:

<?php

$array1=array(1=>"b",0=>"a","num"=>array(0=>"a",1=>"b",0=>"a"),2=>"b");

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

print_r($array1);

echo"<br/><b>New array without duplicate values is:</b><br/>";

print_r(array_unique($array1));

?>

Output:

Initially the values of $array1 is:
Array
(
    [1] => b
    [0] => a
    [num] => Array
        (
            [0] => a
            [1] => b
        )

    [2] => b
)

New array without duplicate values is:
Array
(
    [1] => b
    [0] => a
    [num] => Array
        (
            [0] => a
            [1] => b
        )

)

Example 5:

<?php

$array1=array(1=>"b",0=>"a","num"=>array(0=>"a",1=>"b",0=>"a"),1=>"b");

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

print_r($array1);

echo"<br/><b>New array without duplicate values is:</b><br/>";

print_r(array_unique($array1));

?>

Output:

Initially the values of $array1 is:
Array
(
    [1] => b
    [0] => a
    [num] => Array
        (
            [0] => a
            [1] => b
        )

)

New array without duplicate values is:
Array
(
    [1] => b
    [0] => a
    [num] => Array
        (
            [0] => a
            [1] => b
        )

)
Advertisement

Related Tags for PHP Array Unique:


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.

Posted on: October 30, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com