How to delete an array element based on key in PHP?

Nov 24, 2022

.

Admin

How to delete an array element based on key in PHP?

Hi Dev,

In this article, we will discuss how to delete an array element based on key in PHP. We can get the delete array elements by using array_unique() function. This function will remove the duplicate values from the array.

There are example to check if remove duplicate values from array in php. in this example, we will use to unset() function to check if How to delete an array element based on key in PHP . so let's the following example with output.

Example 1:


index.php

<?php

// Declare arr variable

// and initialize it

$arr = array('G', 'E', 'E', 'K', 'S');

// Display the array element

print_r($arr);

// Use unset() function delete

// elements

unset($arr[2]);

// Display the array element

print_r($arr);

?>

Output:

Array

(

[0] => G

[1] => E

[2] => E

[3] => K

[4] => S

)

Array

(

[0] => G

[1] => E

[3] => K

[4] => S

)

I hope it could help you...

#PHP