Tech

How to Delete an Element by Key from an Array in PHP

PHP Delete Key Array Featured

PHP gives you with many strategies to control arrays. One sort of array manipulation we might look to do is delete a component by its key. Our array might be a sequential listing of values in PHP, nevertheless it is also a set of key-value pairs. Let’s check out the way to delete a component by key from an array in PHP. 

Answer

First, we will check out the key-value instance. 

$array = array('good day' => 'world', 'instance' => 'worth', 'one' => 'extra');

On this instance, now we have a set of key-value pairs within the array. We wish to take away the key-value pair with the important thing of “instance“. So as to do that, we’ll use the unset PHP method

unset($array['example']);

This straightforward technique will take away the worth by its key, however it’s going to additionally take away a component from a sequential array. 

$array = array('A','B','C');

This instance is a sequential array. We will take away any aspect by its place as properly. If we needed to take away the second aspect “B” from the listing, we might use the next code. 

unset($array[1])

That’s all there’s to it. A easy one-liner does the trick to delete a component by key from an array in PHP.

Try our different PHP how-to’s:

How to check if an array has more than one element in PHP

https://techozu.com/how-to-delete-an-element-by-key-from-an-array-in-php/ | Easy methods to Delete an Ingredient by Key from an Array in PHP

Skyred

Inter Reviewed is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – admin@interreviewed.com. The content will be deleted within 24 hours.

Related Articles

Leave a Reply

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

12 + nineteen =

Back to top button