PHP array_pop Function – How to remove elements from and array php

PHP array_pop example tutorial you will learn how to remove elements/values from array with key in PHP.

Here we have added the examples of how to remove array inside array, remove an element from array PHP, remove the key and value from associative array PHP, remove array from multidimensional array PHP etc.

How to remove elements or values from an array in PHP

Use the below given steps to remove elements from types of array in PHP:

  • PHP Remove element from array
  • Remove key from associative array php
  • Remove array from multidimensional array PHP

You can use PHP array_pop() function to removes/deletes the last elements of the end of an array.

PHP array_pop() function

The array_pop() function deletes the last element of an array.

Syntax:

array_pop(array)

PHP Remove element from array

<?php
 
 $array = array("php", "laravel", "codeigniter");
 array_pop($array);
 
 print_r($array);
?>

Output:

Array ( 
  [0] => php 
  [1] => laravel 
)

Remove key from associative array php

If we want to removes/deletes the elements/values into an array with a key. You can use the below code:

<?php
 
 $array = array("key1" => "value1", "key2" => "value2");

 unset($array['key1']);
 print_r($array);
?>

Output:

Array ( 
  [key2] => value2 
)

Here we will remove/delete the elements/values in the array with the key without using array_pop function.

How to remove key from associative array PHP or how to remove the particular key from an array in PHP:

<?php
 
 $array = array("a"=>"red","b"=>"green","c"=>"brown","d"=>"yellow"); 
 unset($array['c']);
 
 print_r($array);

Output;

Array ( 
  [a] => red
  [b] => green
  [d] => yellow 
)

Remove array from multidimensional array PHP

If we want to remove/delete values/elements in a multi-dimensional array. Here we will take an example to remove/delete the values/elements in a multidimensional array.

$array = [
     'web' => ['html', 'css', 'bootstrap'],
     'p_lang' => ['php', 'python', 'cabbage'],
     'framework' => ['laravel', 'codeigniter']
 ];

And you want to remove/delete values/elements inside the array elements. You can use the below example for remove/delete the values/elements in the multidimensional array:

<?php
 
 $array = [
     'web' => ['html', 'css', 'bootstrap'],
     'p_lang' => ['php', 'python', 'cabbage'],
     'framework' => ['laravel', 'codeigniter']
 ];
 
 array_pop($array);
 
 print_r($array);
 
?>

Output:

Array ( [web] => Array ( [0] => html [1] => css [2] => bootstrap ) [p_lang] => Array ( [0] => php [1] => python [2] => cabbage ) )

Remove array inside array PHP with key

If we want to remove/delete values/elements in a multi-dimensional array. Here we will take an example to remove/delete the values/elements in a multidimensional array.

<?php
 
 $array = [
     'web' => ['html', 'css', 'bootstrap'],
     'p_lang' => ['php', 'python', 'cabbage'],
     'framework' => ['laravel', 'codeigniter']
 ];
 
 unset($array['p_lang']);
 
 print_r($array);
 
?>

Output:

Array ( 
  [web] => Array ( 
    [0] => html [1] => css [2] => bootstrap 
  ) 
[framework] => Array ( 
  [0] => laravel 
  [1] => codeigniter ) 
)

So today we learn how to remove or delete elements/values, array from array in PHP. Hope you enjoy this examples..

Leave a Comment