You are here: Home / Topics / How to remove a specific element from an array in PHP

How to remove a specific element from an array in PHP

Filed under: PHP on 2023-07-02 21:26:17

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 
)


About Author:
Y
Yatendra Sir     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.