, ,

How to Delete an element from an array? explain with example

Posted by

Delete an element from an array

There are multiple ways to delete an element from an array in PHP. This query gives some of the most common methods used in PHP to delete an element from an array.

The functions we have used:

unset(): This function takes an element as a parameter and unset it. It wouldn’t change the keys of other elements.
array_splice(): This function takes three parameters an array, offset (where to start), and length (number of elements to be removed). It will automatically re-index an indexed array but not an associated array after deleting the elements.
array_diff(): This function takes an array and list of array values as input and deletes the giving values from an array. Like unset() method, it will not change the keys of other elements.
Steps should be taken:

  • Declare an associated array.
  • Delete an element from an array.
  • Print the result.
  • Declare an indexed array.
  • Delete an element from the indexed array.
  • Print the result.


Example 1: This example depicts the unset() function to delete the element. The unset() function takes the array as a reference and does not return anything.

<?php
 
  // Declaring associated array
  $ass_arr = ["a"=>"Techs", "b"=>"For", "c"=>"Techs"];
 
  // Deleting element associated with key "b"
  unset($ass_arr["b"]);
 
  // Printing array after deleting the element
  print_r($ass_arr);
 
  // Declaring indexed array
  $ind_arr = ["Techs","For","Techs"];
 
  // Deleting element and index 1
  unset($ind_arr[1]);
 
  // Printing array after deleting the element
  print_r($ind_arr);
?>

Output
Array
(
[a] => Techs
=> Techs
)
Array
(
[0] => Techs
[2] => Techs
)

From the output, you can see that unset() has not changed the index for other elements in indexed array.

Example 2: This example depicts array_splice() function to delete an element from an array.

<?php
 
  // Declaring associated array
  $ass_arr = ["a"=>"Techs", "b"=>"For", "c"=>"Techs"];
 
  // Deleting element associated with key "b"
  array_splice($ass_arr,1,1);
 
  // Printing array after deleting the element
  print_r($ass_arr);
 
  // Declaring indexed array
  $ind_arr = ["Techs","For","Techs"];
 
  // Deleting element and index 1
  array_splice($ind_arr,1,1);
 
  // Printing array after deleting the element
  print_r($ind_arr);
?>

Output
Array
(
[a] => Techs
=> Techs
)
Array
(
[0] => Techs
[1] => Techs
)

Example 3: This example depicts array_diff() function to delete the elements. This function takes the array parameters by value not reference and returns an array as output.

<?php
 
  // Declaring associated array
  $ass_arr = ["a"=>"Techs", "b"=>"For", "c"=>"Techs"];
 
  // Deleting element associated with key "b"
  $ass_arr = array_diff($ass_arr,["For"]);
 
  // Printing array after deleting the element
  print_r($ass_arr);
 
  // Declaring indexed array
  $ind_arr = ["Techs","For","Techs"];
 
  // Deleting element and index 1
  $ind_arr = array_diff($ind_arr,["For"]);
 
  // Printing array after deleting the element
  print_r($ind_arr);
?>

Output
Array
(
[a] => Techs
=> Techs
)
Array
(
[0] => Techs
[2] => Techs
)

guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x