TheDeveloperBlog.com

Home | Contact Us

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

How to remove first element from an array in PHP?

How to remove first 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 first element from an array in PHP?

To remove the first element or value from an array, array_shift() function is used. This function also returns the removed element of the array and returns NULL if the array is empty. After removing first element, the key of the other elements is modified, and again array is numbered from beginning, only if the keys are numerical.

It is an inbuilt array function of PHP, which shifts an element from the beginning of the array.

Returns Value

The array_shift() function, which is used to remove the first element from an array, returns the removed element. It also returns NULL, if the array is empty.

For example: Using string elements

<?php
	$color = array("Blue", "Red", "Black", "Green", "Gray", "White");
	echo "Arraylist: ";
	print_r($color);
	$remove = array_shift($color);
	echo "</br>Removed element from array is: ";
	print_r($remove);
	echo "</br> Updated arraylist: ";
	print_r($color);
?>	

Output

An element "Blue" is removed from the first position in the given array, and updated list is displayed in the given output.

Arraylist: Array ( [0] => Blue [1] => Red [2] => Black [3] => Green [4] => Gray [5] => White ) 
Removed element from array is: Blue
Updated arraylist: Array ( [0] => Red [1] => Black [2] => Green [3] => Gray [4] => White )

Example: Using numeric keys

<?php
	$game = array(1 => "Carom", 2 => "Chess", 3 => "Ludo");
	echo "Removed element: ".array_shift($game). "</br>";
	print_r($game);
?>	

Output

Removed element: Carom
Array ( [0] => Chess [1] => Ludo )

Example: Using numeric values

<?php
	$numbers = array(25, 12, 65, 37, 95, 38, 12);
	$removed = array_shift($numbers);
	echo "Removed array element is: ". $removed; 
	echo "</br> Update array is: ";
	print_r($numbers);
?>

Output

An element 25 is removed from the first position in the given array, and updated list is displayed below.

Removed array element is: 25
Update array is: Array ( 
[0] => 12 
[1] => 65 
2] => 37 
[3] => 95 
[4] => 38 
[5] => 12 
)





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