Home » PHP » How to Delete The Last Element from an Associative Array in PHP?

How to Delete The Last Element from an Associative Array in PHP?

Here is how to delete the last element from an associative array in PHP.

PHP supports a predefined function array_shift(), which helps us to delete the first element from an associative array.

Syntax of array_shift() function is given below:

array_shift(arrayvariable name);

The below is an example for deleting the first element from an associative array $a.

<html>
<body>
<?php
$a=array("person1"=>"amit","person2"=>"tarak","person3"=>"monali","person4"=>"raja");
array_shift($a);
print_r($a);
?>
</body>
</html>

Output:Array ( [person2] => tarak [person3] => monali [person4] => raja )

The associative array $a is containg 4 elements. The first element is deleted using the function array_shift(). After deleting the first element, the associative array $a is containing 3 elements and the function print_r is using to print the associative array $a.

See also: