TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

How to remove last element from an array in PHP?

How to remove last element from an array in PHP with examples, php file, php session, php date, php array, php form, functions, time, xml, ajax, php mysql, regex, string, oop, chop(), bin2hex(), addslashes(), addcslashes() etc.

<< Back to HOW

How to remove last element from an array in PHP?

To remove the last element or value from an array, array_pop() function is used. This function returns the last removed element of the array and returns NULL if the array is empty, or is not an array. After removing the last element from the array, the array is modified, and the index is reduced by one from last, only if the keys are numerical.

It is an inbuilt array function of PHP, which deletes the last element of an array.

Returns Value

The array_pop() function, which is used to remove the last element from an array, returns the removed element. It also returns NULL, if the array is empty, or is not an array.

For example: Using string elements

<?php
	$car = array("Mercedes", "Creta", "Audi", "Chevrolet", "Skoda");
	echo "Arraylist: ";
	print_r($car);
	$remove = array_pop($car);
	echo "</br>Removed element from array is: ";
	print_r($remove);
	echo "</br> Updated arraylist: ";
	print_r($car);
?>	

Output

An element "Skoda" is removed from the end of the given array, and the updated list is displayed in the given output.

Arraylist: Array ( [0] => Mercedes [1] => Creta [2] => Audi [3] => Chevrolet [4] => Skoda ) 
Removed element from array is: Swift
Updated arraylist: Array ( [0] => Mercedes [1] => Creta [2] => Audi [3] => Chevrolet )

Example: Using numeric keys

<?php
	$mall = array(1 => "DLF", 2 => "GIP", 3 => "V3S");
	echo "Removed element: ".array_pop($a). "</br>";
	print_r($mall);
?>	

Output

Removed element: Jupiter
Array ( [1] => DLF [2] => GIP )

Example: Using numeric values

<?php
	$numbers = array(45, 76, 23, 91, 82, 39);
	$removed = array_pop($numbers);
	echo "Removed array element is: ". $removed; 
	echo "</br> Update array is: ";
	print_r($numbers);
?>	

Output

An element 39 is removed from the end of the given array, as you can see in the below output.

Removed array element is: 39
Update array is: Array ( [0] => 45 [1] => 76 [2] => 23 [3] => 91 [4] => 82 )





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf