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

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

  • PHP
  • 1 min read

Here is a PHP program example to how to delete the first element from an Associative Array.

A predefined function array_pop() in PHP helps us to delete the first element from an associative array.

The Syntax of array_pop() function is:

array_pop(arrayvariable name);

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

<html>
<body>
<?php
$a=array("color1"=>"red","color2"=>"green","color3"=>"blue","color4"=>"yellow");
array_pop($a);
print_r($a);
?>
</body>
</html>

Output: Array ( [color1] => red [color2] => green [color3] => blue )

This program is using an associative array $a, which is containing 4 elements firstly. After the deletion of the first element using the function array_pop(), the associative array $a is containing 3 elements and print the associative array $a using the predefined function print_r().