Coding Villa

Get unique values from Array using array_unique in PHP

Author : Tahreem Anwar

Share with Friends
Views : 235
Ratings : 0
Bookmarks : 0
Farourities : 0

Description


In this simple article you will learn about Get Unique values from Array using array_unique in PHP.


Advertisements


 

 

Purpose:

 

Array_unique function gets only one parameter that is array. array_unique function remove duplicate values from an array. The duplicate value’s first occurrence is kept and other is truncated.

 

Code:

 

 

<?php

$name=array("T"=>"Tahreem","R"=>"Rizwan","A"=>"Anwar","D"=>"Rizwan");

print_r(array_unique($name));

?>

 

 

Output:

 

 

Array ( [T] => Tahreem [R] => Rizwan [A] => Anwar )

 

 

Explanation:

 

 

On line 2 we have declared an array $name and initialize it by values. Then pass it to array_unique function that removes duplicate values from it. On line 2 we have written value Rizwan two times so it removes one time by array_unique function. Then on line 3 we have passed it to print_r function that traverse entire array and print it.

 



Article Tags
Share with Friends

Comments




Leave a Reply

Name (required)  
Mail (will not be published) (required)   
 
Enter This Code
Captcha
 

Advertisements

Article Categories

.NET


Databases


Mobile Development


Operating Systems


Web Development


Coding Villa on Facebook