Remove Element, Value from Array PHP – array_pop

Remove Element, Value from Array PHP – array_pop

PHP remove element, value from array in PHP. In this tutorial, we will learn how to remove elements/values, array from array in PHP with example.

Here we will learn, how to remove array inside array PHP, remove an element from array PHP, remove the key from associative array PHP, remove array from multidimensional array PHP, how to remove the particular key from an array in PHP, remove array inside array PHP.

Remove elements/values, array to array In PHP

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

Let’s know about the PHP array_pop() function, like array_pop function definition, syntax, and examples:

PHP array_pop() function

The PHP array_pop() function is used to removes/deletes elements to the end of an array.

Syntax

array_pop(array);

Remove element from array php

In this example, we have one array “array(“PHP”, “laravel”, “codeigniter”,”bootstrap”)”, it contains value like (“PHP”, “laravel”, “codeigniter”,” bootstrap “). If we want to remove/delete elements in the array. You can removes/deletes the elements/values into array see below examples:

<?php

 $array = array("php", "laravel", "codeigniter","bootstrap");

 //before remove elements array
 echo "Before add the value:- ";
 print_r($array); echo "<br>";

 //remove elements from array
 array_pop($array);

 //after remove elements from array
 echo "After add the value:- ";

 print_r($array);
 
?>

Remove key from associative array php

Now we have one new array like this ” $array = array(“a”=>”red”,”b”=>”green” ,”c”=>”brown”, “d”=>”yello”, ); “. If we want to removes/deletes the elements/values into an array with a key. You can use the below code:

 $array = array("key1" => "value1", "key2" => "value2");
 print_r($array);
 unset($array['key1']);
 print_r($array);

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"=>"yello");

 //before adding new values 
 echo "Before removes/deletes the elements from array:- ";
 print_r($array);
 echo "<br>";

// add the values in array without using array function
unset($array['c']);


 //after adding a new values
 echo "After removes/deletes the elements from array:- ";
 print_r($array);

?>

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.

If you have a multidimensional array like this:

$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']
 ];


 //before adding new values 
 echo "Before removes/deletes the elements from array:- ";
 print_r($array);
 echo "<br>";

// add the values in array without using array function
array_pop($array);


 //after adding a new values
 echo "After removes/deletes the elements from array:- ";
 print_r($array);

?>

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.

If you have a multidimensional array like this:

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

And you want to remove/delete array inside the array with the key in PHP. You can use the below example for remove/delete array inside array with key:

<?php

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


 //before adding new values 
 echo "Before removes/deletes the elements from array:- ";
 print_r($array);
 echo "<br>";

// add the values in array without using array function
unset($array['p_lang']);


 //after adding a new values
 echo "After removes/deletes the elements from array:- ";
 print_r($array);

?>

Conclusion

remove/delete elements/values, array from array in PHP. Here you have learned how to remove array inside array PHP, remove an element from array PHP, remove the key from associative array PHP, remove array from multidimensional array PHP, how to remove the particular key from an array in PHP, remove array inside array PHP.

Leave a Reply

Your email address will not be published. Required fields are marked *