php

How to remove the first element from an array in PHP

In this tutorial, we are going to see how to remove the first element from an array in PHP. You can use PHP’s array_shift() function to remove the first element or the first value from an array. The array_shift() function also returns the value removed from the array. However, if the array is empty (or if the variable is not an array), the returned value will be NULL.
 

How to remove the first element from an array in PHP
<?php
	$languages = array("PHP", "Java", "Python", "HTML", "CSS", "JavaScript");
	 
	//Remove the first element from the array
	$res = array_shift($languages);
	print_r($languages);
	echo "<br>";
	var_dump($res);
?>

Output:

Array (
 [0] => Java 
 [1] => Python 
 [2] => HTML 
 [3] => CSS 
 [4] => JavaScript 
 ) 
 
string(3) "PHP"

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *