php

How to remove a specific element from an array in PHP

In this tutorial, we are going to see how to remove a specific element from an array in PHP. If you want to remove an element from an array, you can just use the PHP unset() method. The following example shows you how to remove an element from an array.
 

Example 1: How to remove a specific element from an associative array by key in PHP
<?php
	$languages = array("p"=>"PHP", "j"=>"Java", "a"=>"Ada", "h"=>"HTML");
	unset($languages["j"]);
	print_r($languages);	
?>

Output:

Array ( 
	[p] => PHP 
	[a] => Ada 
	[h] => HTML 
)
 

Example 2: How to remove a specific element from an array by index in PHP
<?php	 
	$nbrs = array(1, 2, 3, 4, 5);
	unset($nbrs[1]);
	print_r($nbrs);
?>

Output:

Array ( 
	[0] => 1 
	[2] => 3 
	[3] => 4 
	[4] => 5 
)
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 *