How to use array_unique() function in PHP – PHP Core Basics

Introduction to PHP Function – array_unique() function

  • The array_unique() function removes the duplicate values (key-value) from an array. Let say if an array has two or more same values then array_unique() function keeps the first value while others will be removed.
  • The array_unique() function takes an array as input parameter and returns a new array with unique values (removes duplicate values).PHP Tutorials
  • The array_unique() function needs a parameter of type Array. This is required parameter not optional.
  • The array_unique() function does’t work for multi-dimensional arrays.
  • The array_unique() function is used to maintain uniqueness within the array or to avoid the duplicity of values in an array.
Syntax: 
array_unique(array)
For Example-
<?php
$a=array("a"=>"India","b"=>"USA","c"=>"India");
print_r(array_unique($a));
?>
Output:
Array ( [a] => India [b] => USA )

LEAVE A REPLY

CommentLuv badge