php

How to remove duplicates from an array in PHP

In this tutorial, we are going to see how to remove duplicates from an array in PHP. You can use the PHP array_unique() function to remove duplicate elements or values in an array. If the array contains keys of type String, this function will keep the first key encountered for each value and ignore all subsequent keys.
 

How to remove duplicates from an array in PHP
<?php
	$languages = array("PHP", "Java", "PHP", "HTML", "CSS", "Fortran", "HTML");
	 
	// Remove duplicates
	$unique = array_unique($languages);
	print_r($unique);
?>

Output:

Array (
 [0] => PHP 
 [1] => Java 
 [3] => HTML 
 [4] => CSS 
 [5] => Fortran 
 )

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *