PHP

Remove Element From Array In PHP

There are two ways to remove an element from the array in PHP.

  • Unset() Method
  • Array_splice() Method

Unset() Method

The unset() method is a built-in function in PHP that is used to remove or unset a single element from the array. Check out the syntax.

Syntax

unset(variable,….);

In the syntax we have only one parameter which is the variable,  pass the variable with the index value for unsetting or deleting the element from an array.

Array_splice Method

The array_splice function is used when you want to delete or remove or replace an element from the array.

Syntax

array_splice(array, start, length, array);

The function has 3 parameters the first one is an array second is the starting point from where to start deleting the elements in an array and the third one is the length, it is an optional parameter you can tell the length that how many elements you want to delete and the last is an array it is also optional in which you can pass the original array with inserted values which you want to replace.

How to remove element from array in PHP?

Examples using unset() method

Example 1

<?php

  $arr = array("john","mark", "noah", "peter");
  unset($arr[2]);
  print_r($arr);

?>

In the above example, we declared an index array in which we stored some string values then we used the unset method for removing an element from the array, with the keyword Unset we pass the variable name with that index number which we want to unset. 

Let’s check the results.

Output

Array (
 [0] => jhon 
 [1] => mark
 [3] => peter 
) 

As you can see the 2nd index element has been unset or removed from the array.

Example 2

<?php

 $top = ["a"=> "AI", "b"=>"Programming", "c"=>"Python", 
 "d"=> "PHP"];

 unset($top['a']);
 echo "<pre>";
 print_r($top);
 echo "</pre>";

?>

In the above example, we declared an associative array and used the unset method.

Output

Array
(
    [b] => Programming
    [c] => Python
    [d] => PHP
)

Example 3 – Multi-Dimensional Array

<?php

 $top = [
   ['try', 2],
   ['if you want to', 4],
   ['fly', 5],
   ['high',9],
 ];
 unset($top[2]);
 echo "<pre>";
 print_r($top);
 echo "</pre>";

?>

In the above example, we declared a two-dimensional array, and then we use the unset method.

Output


Array
(
    [0] => Array
        (
            [0] => try
            [1] => 2
        )
 
    [1] => Array
        (
            [0] => if you want to
            [1] => 4
        )
 
    [3] => Array
        (
            [0] => high
            [1] => 9
        )
 
)

As you can see 2nd index element has been removed from the array.

Did you notice above in both examples that after deleting an element the array keys numbers did not reindex, mean to say if we are unsetting an element so the next key array should take its place but it can’t be done in the unset function so for this purpose we have another function called array_splice.

Examples using array_splice() method

Example 1

<?php

  $arr = array(5, 4, 3, 2, 1);
  array_splice($arr, 2, 2);
  print_r($arr);

?>

Output

Array ( 
 [0] => 5 
 [1] => 4 
 [2] => 1 
)

As you can see in the output the array keys got reindexed after deleting the elements from the array.

Example 2

<?php

 $top = ["a"=> "AI", "b"=>"Programming", "c"=>"Python", 
 "d"=> "PHP"];
 array_splice($top, 2, 1);
 echo "<pre>";
 print_r($top);
 echo "</pre>";

?>

In the above example, we used an associative array for the array_splice function.

Output

Array
(
    [a] => AI
    [b] => programming
    [d] => php
)

Example 3 – Multi-Dimensional Array

<?php

 $top = [
   ['i', 2],
   ['love', 2],
   ['to', 8],
   ['code', 8],
   ['in php', 1],
 ];
 array_splice($top, 2, 1);

 echo "<pre>";
 print_r($top);
 echo "</pre>";
 

?>

In the above example, we used the array_splice function for a two-dimensional array.

Output

Array
(
    [0] => Array
        (
            [0] => i
            [1] => 2
        )
 
    [1] => Array
        (
            [0] => love
            [1] => 2
        )
 
    [2] => Array
        (
            [0] => code
            [1] => 8
        )
 
    [3] => Array
        (
            [0] => in php
            [1] => 1
        )
 
)

Conclusion

In this tutorial, we discussed the two most helpful methods for removing an element from the array in PHP.

First, we discussed the Unset() method and understood its examples then we discussed the array_splice method and its examples.

Suggested Articles:

Leave a Comment