php

How to remove the last element from an array in PHP

In this tutorial, we are going to see how to remove the last element from an array in PHP. You can use PHP’s array_pop() function to remove an element or a value from the end of an array. The array_pop() function also returns the last value in 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 last element from an array in PHP
<?php
	$languages = array("PHP", "Java", "Python", "HTML", "CSS", "JavaScript");
	 
	// Remove the last element from the array
	$res = array_pop($languages);
	print_r($languages);
	echo "<br>";
	var_dump($res);
?>

Output:

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

string(10) "JavaScript"

 

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 *