function

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

In this short article, we’ll look at a couple of examples of removing array elements by their key.

Full code listing below:

$array = [
	1 => 'One',
	'Two' => 3,
	'Three' => 'Three',
	4 => NULL,
];
echo "<pre dir='ltr'>";print_r($array); echo "</pre>";

if(isset($array['Two']))
{
	unset($array['Two']);
}
echo "<pre dir='ltr'>";print_r($array); echo "</pre>";

if(isset($array[4]))
{
	unset($array[4]);
}
echo "<pre dir='ltr'>";print_r($array); echo "</pre>";

if(array_key_exists(4, $array))
{
	unset($array[4]);
}
echo "<pre dir='ltr'>";print_r($array); echo "</pre>";

read more...