Remove a Specific Element From an Array in PHP
Jun 07, 2022 . Admin

Hello Friends,
Here, I will show you How to delete an Element From an Array in PHP. step by step explain How to remove element from an array in php. I would like to show you How can we delete any element from an array in PHP. step by step explain Deleting Elements from an Array. So, let's follow few step to create example of PHP deleting elements of an array by unset ( key or value ).
This article will give you simple example of Remove a Specific Element From an Array in PHP
So, let's see bellow solution:
Example 1:<?php $flowers = array("Rose","Lili","Jasmine","Hibiscus","Daffodil","Daisy"); echo "Current Array :"; echo "<pre>"; print_r($flowers); echo "</pre>"; echo "<br>"; $value = "Jasmine"; if (($key = array_search($value, $flowers)) !== false) { unset($flowers[$key]); } echo("Array after deletion: \n"); echo"<pre>"; print_r($flowers); echo"</pre>"; ?>Output:
Current Array : Array ( [0] => Rose [1] => Lili [2] => Jasmine [3] => Hibiscus [4] => Daffodil [5] => Daisy ) Array after deletion: Array ( [0] => Rose [1] => Lili [3] => Hibiscus [4] => Daffodil [5] => Daisy )Example 2:
<?php $flowers = array( "Rose", "Lili", "Jasmine", "Hibiscus", "Tulip", ); echo "This is a Current Array :"; echo"<pre>"; print_r($flowers); echo"</pre>"; $flowers = array_diff($flowers, array("Rose","Lili")); echo "After Delete Element:\n"; echo"<pre>"; print_r($flowers); echo"</pre>"; ?>Output:
This is a Current Array : Array ( [0] => Rose [1] => Lili [2] => Jasmine [3] => Hibiscus [4] => Tulip ) After Delete Element: Array ( [2] => Jasmine [3] => Hibiscus [4] => Tulip )
It will help you...